using HotelPms.Share.Util;
|
using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Diagnostics;
|
using System.Drawing;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
|
namespace HotelPms.Share.Windows.Tool
|
{
|
public partial class DataSelector : Form
|
{
|
#region ★★★★★ Declartions ★★★★★
|
|
[Flags]
|
public enum Style : int
|
{
|
None = 0x1,
|
HideBottom = 0x2,
|
HideReturnField = 0x4,
|
HideGridHeader = 0x8,
|
}
|
|
private List<string> m_CurrentRow = new List<string>();
|
|
|
public List<string> SelecetdItem
|
{
|
get
|
{
|
return m_CurrentRow;
|
}
|
}
|
|
private DataView DtView = new DataView();
|
|
// 共通変数
|
private int intReturnField_IndexNo; // ReturnFieldのIndex
|
private bool strVisibleBottom = true;
|
private bool strVisibleReturnField = true;
|
private bool visibleGridHeader = true;
|
|
#endregion
|
|
#region ★★★★★ Property ★★★★★
|
|
public bool IsReturnRow { get; set; } = false;
|
|
public Control OwnerControl { get; set; } = null;
|
|
public DataTable Data { get; set; } = null;
|
|
/// <summary>
|
/// ReturnField(選択したときに返すフィールド名)
|
/// </summary>
|
public string ReturnField { get; set; } = "コード";
|
|
public string SearchField { get; set; } = "名称";
|
|
public Style CurStyle { get; set; } = Style.None;
|
|
public List<int> ColWidth { get; set; } = new List<int>();
|
|
/// <summary>
|
/// ReturnValue(選択後に取得できる値)
|
/// </summary>
|
public string ReturnValue { get; set; } = string.Empty;
|
|
#endregion
|
|
#region ★★★★★ Class Event ★★★★★
|
|
public DataSelector(Control obj)
|
{
|
InitializeComponent();
|
OwnerControl = obj;
|
}
|
|
private void CalendarSelector_Load(object sender, EventArgs e)
|
{
|
//如果要将Control1(例如Label1或者Button1)上的点(x,y)转换成屏幕上的点(x1,y1),那么就调用Control1.PointToScreen。
|
//反之,如果要将屏幕的(x1,y1)变成控件上的(x,y),那么就调用Control1.PointToClient。
|
//eg1:求Button1的左上角在屏幕上的位置。
|
//Point p = new Point(0, 0); // 0,0 是左上角
|
//p = Button1.PointToScreen(p);
|
Point location = OwnerControl.PointToScreen(new Point(0, 0));
|
location = new Point(location.X -1,location.Y + OwnerControl.Height - 2);
|
Screen currentScreen = Screen.FromControl(OwnerControl);
|
if (location.X + this.Width > currentScreen.WorkingArea.X + currentScreen.WorkingArea.Width) { location.X = currentScreen.WorkingArea.X + currentScreen.WorkingArea.Width - this.Width; }
|
if(location.Y+ this.Height > currentScreen.WorkingArea.Y + currentScreen.WorkingArea.Height) { location.Y -= (currentScreen.WorkingArea.Y + OwnerControl.Height - 2 + this.Height); }
|
this.Location = location;
|
|
if ((CurStyle & Style.HideBottom) == Style.HideBottom) { strVisibleBottom = false; }
|
if ((CurStyle & Style.HideGridHeader) == Style.HideGridHeader) { visibleGridHeader = false; }
|
if ((CurStyle & Style.HideReturnField) == Style.HideReturnField) { strVisibleReturnField = false; }
|
|
Data.TableName = "SelectTable";
|
DtView.Table = Data;
|
DtView.RowFilter = "";
|
|
// データを表示
|
this.DataGridView1.DataSource = DtView;
|
|
//下部の検索キーと閉じるボタンを表示するか(初期値:True(表示))
|
pnlFilter.Visible = strVisibleBottom;
|
|
//ReturnField(戻り値を返す項目列名)のIndexNoを取得する。
|
intReturnField_IndexNo = -1;
|
|
if (ReturnField.Length == 0)
|
{
|
intReturnField_IndexNo = 0;
|
}
|
else
|
{
|
for (int i = 0; i < this.DataGridView1.Columns.Count; i++)
|
{
|
if (this.DataGridView1.Columns[i].HeaderText.ToString() == ReturnField.ToString())
|
{
|
intReturnField_IndexNo = i;
|
}
|
}
|
}
|
|
//ReturnField(戻り値を返す項目列名)を取得する。
|
if (ReturnField.Length == 0)
|
{
|
ReturnField = this.DtView.Table.Columns[intReturnField_IndexNo].ColumnName.ToString();
|
}
|
|
//SearchField(検索キーに関連させる項目列名)を取得する。
|
if (SearchField.Length == 0)
|
{
|
SearchField = this.DtView.Table.Columns[intReturnField_IndexNo + 1].ColumnName.ToString();
|
}
|
|
//VisibleReturnField = falseの場合、ReturnFieldを非表示とする。
|
if ((!strVisibleReturnField) && (intReturnField_IndexNo > -1))
|
{
|
this.DataGridView1.Columns[intReturnField_IndexNo].Visible = false;
|
}
|
|
if (Data.Rows.Count == 0)
|
{
|
MessageBox.Show("該当データがありません。");
|
this.Close();
|
return;
|
}
|
|
this.DataGridView1.ColumnHeadersVisible = visibleGridHeader;
|
|
//エラー処理
|
if (!ValidateInput())
|
{
|
Debug.Assert(false, "開発用Error", "Selectが不適切、searchFieldが数値、文字列以外");
|
|
}
|
|
DataGridView1.EnableHeadersVisualStyles = false;
|
DataGridView1.ColumnHeadersHeight = 25;
|
foreach (DataGridViewColumn col in DataGridView1.Columns)
|
{
|
col.HeaderCell.Style.BackColor = System.Drawing.Color.FromArgb(50, 50, 100);
|
col.HeaderCell.Style.ForeColor = Color.White;
|
}
|
|
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.None;
|
DataGridView1.AllowUserToResizeColumns = true;
|
if (ColWidth == null || ColWidth.Count == 0)
|
{
|
DataGridView1.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
|
}
|
else
|
{
|
for (int i = 0; i < ColWidth.Count; i++)
|
{
|
DataGridView1.Columns[i].Width = ColWidth[i];
|
}
|
}
|
|
|
if(DataGridView1.Rows.Count > 0)
|
{
|
DataGridView1.Rows[0].Cells[0].Selected = true;
|
DataGridView1.CurrentCell = DataGridView1.Rows[0].Cells[0];
|
this.ActiveControl = DataGridView1;
|
}
|
}
|
|
private void DataSelector_KeyDown(object sender, KeyEventArgs e)
|
{
|
switch (e.KeyCode)
|
{
|
case Keys.Escape:
|
e.Handled = true;
|
ReturnValue = string.Empty;
|
this.Close();
|
break;
|
}
|
}
|
|
#endregion
|
|
#region ★★★★★ Control Event ★★★★★
|
|
private void DataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
|
{
|
DataGridView1_KeyDown(sender, new KeyEventArgs(Keys.Enter));
|
}
|
|
private void DataGridView1_KeyDown(object sender, KeyEventArgs e)
|
{
|
if (e.KeyCode == Keys.Enter)
|
{
|
if (DataGridView1.RowCount > 0)
|
{
|
|
if (!IsReturnRow)
|
{
|
ReturnValue = this.DataGridView1[ReturnField, this.DataGridView1.CurrentCell.RowIndex].Value.ToString(); //Cell値
|
}
|
else
|
{
|
m_CurrentRow.Clear();
|
foreach (DataGridViewCell cell in DataGridView1.CurrentRow.Cells) { m_CurrentRow.Add(CConvert.ToString(cell.Value)); }
|
}
|
//画面を閉じる
|
this.Close();
|
}
|
}
|
else if (e.KeyCode == Keys.Tab)
|
{
|
e.Handled = true;
|
txtFilter.Focus();
|
}
|
}
|
|
private void TxtFilter_KeyDown(object sender, KeyEventArgs e)
|
{
|
//Enterキー入力の場合
|
if (e.KeyCode == Keys.Enter)
|
{
|
//DataGridView1へフォーカス移動
|
DataGridView1.Focus();
|
}
|
}
|
|
private void TxtFilter_Enter(object sender, EventArgs e)
|
{
|
txtFilter.BackColor = Color.LemonChiffon;
|
}
|
|
private void TxtFilter_Leave(object sender, EventArgs e)
|
{
|
txtFilter.BackColor = Color.White;
|
}
|
|
private void TxtFilter_TextChanged(object sender, EventArgs e)
|
{
|
// フィルター条件を判別
|
if (txtFilter.Text.Length == 0 || SearchField.Length == 0)
|
{
|
//フィルタ条件無し(全てを表示)
|
DtView.RowFilter = string.Empty;
|
}
|
else
|
{
|
//フィルタ条件指定
|
DtView.RowFilter = $"{SearchField} LIKE '%{txtFilter.Text}%'";
|
}
|
// データを表示
|
this.DataGridView1.DataSource = DtView;
|
}
|
|
|
#endregion
|
|
#region ★★★★★ Private Function ★★★★★
|
|
/// <summary>
|
/// ValidateInput
|
/// </summary>
|
private bool ValidateInput()
|
{
|
if (IsReturnRow) { return true; }
|
if (Data.Rows.Count == 0) { return false; }
|
if (Data.Columns[SearchField].DataType != typeof(string)) { return false; }
|
return true;
|
}
|
|
#endregion
|
|
#region ★★★★★ Public Function ★★★★★
|
|
public static List<string> ExecuteForRow(Control owner, DataTable data, Style style, Size size, string returnField, string searchField, List<int> colWidth)
|
{
|
using (DataSelector form = new DataSelector(owner))
|
{
|
form.CurStyle = style;
|
if (!size.Equals(Size.Empty)) { form.Size = size; }
|
if (returnField.Length > 0) { form.ReturnField = returnField; }
|
if (searchField.Length > 0) { form.SearchField = searchField; }
|
if (colWidth != null) { form.ColWidth = colWidth; }
|
form.Data = data;
|
form.IsReturnRow = true;
|
form.ShowDialog();
|
return form.SelecetdItem;
|
}
|
|
}
|
|
/// <summary>
|
/// 一覧表示
|
/// </summary>
|
/// <returns></returns>
|
public static string Execute(Control owner, DataTable data)
|
{
|
return Execute(owner, data, Size.Empty);
|
}
|
|
public static string Execute(Control owner, DataTable data, Size size)
|
{
|
return Execute(owner, data, size, string.Empty, string.Empty, null, Style.None);
|
}
|
|
public static string Execute(Control owner, DataTable data, Size size, string returnField, string searchField, List<int> colWidth, Style style)
|
{
|
string result = string.Empty;
|
using (DataSelector form = new DataSelector(owner))
|
{
|
form.CurStyle = style;
|
if (!size.Equals(Size.Empty)) { form.Size = size; }
|
if (returnField.Length > 0) { form.ReturnField = returnField; }
|
if (searchField.Length > 0) { form.SearchField = searchField; }
|
if (colWidth != null) { form.ColWidth = colWidth; }
|
form.Data = data;
|
form.ShowDialog();
|
if (form.ReturnValue.Length > 0)
|
{
|
result = form.ReturnValue;
|
}
|
}
|
return result;
|
}
|
|
#endregion
|
}
|
}
|