using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using ChatworkBulkSender.Utils;
namespace ChatworkBulkSender.UserControls
{
[DesignerCategory("UserControl")]
public partial class SearchBoxControl : BaseSearchControl
{
public Button BtnSearch => _btnSearch;
public CheckBox CHkUnusedData => _chkUnusedData;
public TextBox MgmtNumber => _searchTextBoxes.ContainsKey("ManagementNumber") ? _searchTextBoxes["ManagementNumber"] : null;
public TextBox CustomerNameTextBox => _searchTextBoxes.ContainsKey("CustomerName") ? _searchTextBoxes["CustomerName"] : null;
// エラー表示用ツールチップ
private ToolTip errorToolTip = new ToolTip();
public SearchBoxControl()
{
InitializeComponent();
InitializeControls();
base._panel = this.panel;
base._lblSearchBox = this.lblSearchBox;
base._panelSearchBox = this.panelSearchBox;
base._btnSearch = this.btnSearch;
base._chkUnusedData = this.chkUnusedData;
base._searchTextBoxes["ManagementNumber"] = this.mgmtNumberTxtBox;
base._searchTextBoxes["CustomerName"] = this.customerNameTxtBox;
// 管理番号テキストボックスのイベントハンドラを設定
this.mgmtNumberTxtBox.TextChanged += MgmtNumberTxtBox_TextChanged;
this.mgmtNumberTxtBox.KeyPress += MgmtNumberTxtBox_KeyPress;
// 各テキストボックスの最大文字数を設定
this.mgmtNumberTxtBox.MaxLength = 10; // 管理番号は10桁まで
this.customerNameTxtBox.MaxLength = 100; // 顧客名は100文字まで
this._btnSearch.Click += base.BtnSearch_Click;
this.ActiveControl = null;
}
protected override void InitializeControls()
{
if (_btnSearch != null)
{
_btnSearch.Click += base.BtnSearch_Click;
}
}
protected override Panel CreateSearchFIeldsPanel()
{
// 既存デザイナーで作成済みのレイアウトを使用するため、nullを返す
return null;
}
protected override void AdjustLayout()
{
if (IsInDesignMode()) { return; }
// Null チェック
if (_panelSearchBox == null || _btnSearch == null || _chkUnusedData == null)
return;
// サイズが0の場合もスキップ
if (_panelSearchBox.Width <= 0 || _panelSearchBox.Height <= 0)
return;
// フォントサイズに合わせて高さを調整(165px → 200px)
_panelSearchBox.Height = 200;
// 検索条件ラベルの位置を元の位置に保つ
if (_lblSearchBox != null)
{
_lblSearchBox.Location = new System.Drawing.Point(36, 26);
}
// 基底クラスのレイアウト調整を呼び出し(ボタンとチェックボックスの位置は基底クラスで設定)
base.AdjustLayout();
}
protected override bool ValidateSearchCriteria(out string errorMessage)
{
errorMessage = string.Empty;
var errors = new List<string>();
// 管理番号のバリデーション
if (!string.IsNullOrWhiteSpace(mgmtNumberTxtBox.Text))
{
if (!ValidationHelperUtil.IsValidManagementNumber(mgmtNumberTxtBox.Text))
{
errors.Add(ValidationHelperUtil.GetManagementNumberFormatErrorMessage());
}
}
if (errors.Any())
{
errorMessage = string.Join("\n", errors);
return false;
}
return true;
}
protected override void BtnSearch_Click(object sender, EventArgs e)
{
base.BtnSearch_Click(sender, e);
}
/// <summary>
/// 管理番号テキストボックスのKeyPressイベントハンドラ
/// </summary>
private void MgmtNumberTxtBox_KeyPress(object sender, KeyPressEventArgs e)
{
// バックスペース、Delete、Ctrl+V は許可
if (e.KeyChar == '\b' || char.IsControl(e.KeyChar))
{
return;
}
// 数字以外の入力時
if (!char.IsDigit(e.KeyChar))
{
e.Handled = true;
// ツールチップでエラーメッセージを表示
errorToolTip.Show("半角数字のみ入力可能です", mgmtNumberTxtBox, 0, -25, 2000);
}
}
/// <summary>
/// 管理番号テキストボックスのTextChangedイベントハンドラ
/// </summary>
private void MgmtNumberTxtBox_TextChanged(object sender, EventArgs e)
{
// 先頭のゼロを削除する処理
if (!string.IsNullOrWhiteSpace(mgmtNumberTxtBox.Text))
{
string text = mgmtNumberTxtBox.Text;
// 先頭の0を削除(001 → 1)
if (text.Length > 1 && text.StartsWith("0"))
{
if (int.TryParse(text, out int number))
{
mgmtNumberTxtBox.Text = number.ToString();
// カーソルを末尾に移動
mgmtNumberTxtBox.SelectionStart = mgmtNumberTxtBox.Text.Length;
}
}
// バリデーション
if (!ValidationHelperUtil.IsValidManagementNumber(mgmtNumberTxtBox.Text))
{
// 背景色を薄い赤にして視覚的にエラーを示す
mgmtNumberTxtBox.BackColor = Color.FromArgb(255, 230, 230);
errorToolTip.Show(ValidationHelperUtil.GetManagementNumberFormatErrorMessage(),
mgmtNumberTxtBox, 0, -25, 3000);
}
else
{
// 正常な場合は背景色を元に戻す
mgmtNumberTxtBox.BackColor = SystemColors.Window;
errorToolTip.Hide(mgmtNumberTxtBox);
}
}
else
{
// 空の場合も背景色を元に戻す
mgmtNumberTxtBox.BackColor = SystemColors.Window;
errorToolTip.Hide(mgmtNumberTxtBox);
}
}
}
}