using System;
|
using System.ComponentModel;
|
using System.Collections.Generic;
|
using System.Text;
|
using System.Windows.Forms;
|
using HotelPms.Share.Util;
|
using System.Drawing;
|
using HotelPms.Share.Windows.Component;
|
|
namespace HotelPms.Share.Windows.Util
|
{
|
public class CTextBoxArray
|
{
|
private class ItemsComparer : IComparer<CTextBox>
|
{
|
public int Compare(CTextBox x, CTextBox y)
|
{
|
if (x == null)
|
{
|
return (y == null ? 0 : -1);
|
}
|
else
|
{
|
if (y == null)
|
{
|
return 1;
|
}
|
else
|
{
|
return x.TabIndex.CompareTo(y.TabIndex);
|
}
|
}
|
}
|
}
|
private ItemsComparer itemsComparer = new ItemsComparer();
|
|
public delegate void TBArrayEventHandler(object sender, int index, CTextBoxArrayBaseEventArgs e);
|
public delegate bool TBArrayResultEventHandler(object sender, int index, CTextBoxArrayBaseEventArgs e);
|
public delegate void TBArrayValidatingEventHandler(object sender, int index, CancelEventArgs e);
|
public delegate void TBArrayKeyDownEventHandler(object sender, int index, KeyEventArgs e);
|
public delegate void TBArrayPreviewKeyDownEventHandler(object sender, int index, PreviewKeyDownEventArgs e);
|
public delegate void TBArrayKeyPressEventHandler(object sender, int index, KeyPressEventArgs e);
|
|
public event TBArrayKeyPressEventHandler KeyPress;
|
public event TBArrayKeyDownEventHandler KeyDown;
|
public event TBArrayPreviewKeyDownEventHandler PreviewKeyDown;
|
public event TBArrayKeyDownEventHandler BeforeAutoNextFocus;
|
public event TBArrayKeyDownEventHandler AfterAutoNextFocus;
|
public event TBArrayValidatingEventHandler Validating;
|
public event TBArrayEventHandler TextChanged;
|
public event TBArrayEventHandler Enter;
|
public event TBArrayEventHandler AfterEnter;
|
public event TBArrayEventHandler Leave;
|
public event TBArrayEventHandler AfterLeave;
|
public event TBArrayEventHandler Validated;
|
|
public event TBArrayEventHandler ValueChanged; //üÍÚÏXµ½©Ç¤©
|
public event TBArrayResultEventHandler IsCorrectEtc; //Rg[vpeBÈOÌ`FbNðs¤Æ«É±ÌCxgàÅs¤B
|
public event TBArrayKeyDownEventHandler NextFocus;
|
public event TBArrayKeyDownEventHandler KeyEnd;
|
public event TBArrayEventHandler ForceValidating;
|
public event TBArrayKeyPressEventHandler MultiKeyPress;
|
|
private List<CTextBox> items = new List<CTextBox>();
|
private string orgMstName = string.Empty;
|
private int acitveIndex = 0;
|
private Color orgBkColor = Color.White;
|
private bool isChangeLine = false;
|
private bool disbleValidating = false;
|
private bool disbleTextChanged = false;
|
private bool autoNextFocus = false;
|
private bool m_ReadOnly = false;
|
|
private bool m_ForceChangeLine = false;
|
|
/// <summary>
|
/// }`üÍÌêAuEntervÅüs
|
/// </summary>
|
public bool ForceChangeLine
|
{
|
get { return m_ForceChangeLine; }
|
set { m_ForceChangeLine = value; }
|
}
|
|
private bool m_UsingOrgBackColor = false;
|
|
public bool UsingOrgBackColor
|
{
|
get { return m_UsingOrgBackColor; }
|
set { m_UsingOrgBackColor = value; }
|
}
|
|
/// <summary>
|
///
|
/// </summary>
|
public bool ReadOnly
|
{
|
get { return m_ReadOnly; }
|
set
|
{
|
try
|
{
|
m_ReadOnly = value;
|
foreach (CTextBox item in items) { item.ReadOnly = m_ReadOnly; }
|
}
|
catch { }
|
}
|
}
|
|
|
/// <summary>
|
/// ³Ì}X^¼itHbJXÌj
|
/// </summary>
|
public string OrgMstName
|
{
|
get { return orgMstName; }
|
set { orgMstName = value; }
|
}
|
|
public List<CTextBox> Items
|
{
|
get { return items; }
|
}
|
|
public bool DisbleValidating
|
{
|
get { return disbleValidating; }
|
set { disbleValidating = value; }
|
}
|
|
public bool DisbleTextChanged
|
{
|
get { return disbleTextChanged; }
|
set { disbleTextChanged = value; }
|
}
|
|
/// <summary>
|
/// TABÊÅtH[JX·é©Ç¤©
|
/// </summary>
|
public bool AutoNextFocus
|
{
|
get { return autoNextFocus; }
|
set { autoNextFocus = value; }
|
}
|
|
private bool m_ShiftEnterEnabled = false;
|
|
/// <summary>
|
/// ShiftEnter = KeyUp ?
|
/// </summary>
|
public bool ShiftEnterEnabled
|
{
|
get { return m_ShiftEnterEnabled; }
|
set { m_ShiftEnterEnabled = value; }
|
}
|
|
|
public CTextBoxArray()
|
: this(false, null)
|
{ }
|
|
public CTextBoxArray(bool autoNextFocus)
|
: this(autoNextFocus, null)
|
{ }
|
|
public CTextBoxArray(bool autoNextFocus, Control owner)
|
{
|
this.autoNextFocus = autoNextFocus;
|
if (owner != null) { InitInputSetting(owner); }
|
}
|
|
public void Add(CTextBox item)
|
{
|
Add(item, -1);
|
}
|
|
public void Add(CTextBox item, int index)
|
{
|
item.Enter += new EventHandler(this.Cell_Enter);
|
item.Leave += new EventHandler(this.Cell_Leave);
|
item.KeyPress += new KeyPressEventHandler(this.Cell_KeyPress);
|
item.KeyDown += new KeyEventHandler(Cell_KeyDown);
|
item.PreviewKeyDown += new PreviewKeyDownEventHandler(Cell_PreviewKeyDown);
|
item.TextChanged += new EventHandler(this.Cell_TextChanged);
|
item.Validating += new CancelEventHandler(this.Cell_Validating);
|
item.Validated += new EventHandler(this.Cell_Validated);
|
item.DoubleClick += new EventHandler(Cell_DoubleClick);
|
if (index >= 0)
|
{
|
items.Insert(index,item);
|
}
|
else
|
{
|
items.Add(item);
|
}
|
//enableManager.Add(new ControlEnableManager(item));
|
}
|
|
private void Cell_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
|
{
|
int index = items.IndexOf((sender as CTextBox));
|
if (PreviewKeyDown != null)
|
{
|
PreviewKeyDown(this, index, e);
|
}
|
}
|
|
|
public static void SetCtrlEnabled(Control obj,bool value)
|
{
|
try
|
{
|
if (obj is CTextBox)
|
{
|
(obj as CTextBox).Enabled = value;
|
}
|
else
|
{
|
obj.Enabled = value;
|
}
|
}
|
catch { }
|
}
|
|
|
/// <summary>
|
/// æÊÉüÍÚÈOÌCTextBoxðüÍsÂÉ·é
|
/// </summary>
|
/// <param name="obj">eeí</param>
|
/// <param name="tbArray">üÍÇÚ</param>
|
public void SetCTextProtect(Control obj)
|
{
|
foreach (Control item in obj.Controls)
|
{
|
if (item is UserControl) { continue; }
|
|
if (item is CTextBox)
|
{
|
if (!items.Contains(item as CTextBox)) { (item as CTextBox).Enabled = false; }
|
}
|
|
SetCTextProtect(item);
|
}
|
}
|
|
public void ClearText()
|
{
|
foreach (CTextBox item in items)
|
{
|
item.Text = string.Empty;
|
if (item.MstNameCtrl != null)
|
{
|
item.MstNameCtrl.Text = string.Empty;
|
}
|
}
|
}
|
|
public void ClearTag()
|
{
|
foreach (CTextBox item in items)
|
{
|
item.Tag = null;
|
}
|
}
|
|
public void PerformValidated(object sender, EventArgs e)
|
{
|
Cell_Validated(sender, e);
|
}
|
|
public void PerformValidating(object sender, CancelEventArgs e)
|
{
|
Cell_Validating(sender, e);
|
}
|
|
public void PerformEnter(object sender, EventArgs e)
|
{
|
Cell_Enter(sender, e);
|
}
|
|
public void PerformEnter()
|
{
|
Cell_Enter(items[acitveIndex], new EventArgs());
|
}
|
|
public void PerformKeyDown(object sender, KeyEventArgs e)
|
{
|
Cell_KeyDown(sender, e);
|
}
|
|
private void SetItemValue(object sender, int index, string value)
|
{
|
(sender as CTextBox).TextChanged -= new EventHandler(Cell_TextChanged);
|
this.Items[index].Text = value;
|
(sender as CTextBox).TextChanged += new EventHandler(Cell_TextChanged);
|
}
|
|
private string GetItemValue(object sender, int index)
|
{
|
string value = string.Empty;
|
value = this.Items[index].Text;
|
return value;
|
}
|
|
private bool CheckValueChanged(object sender, int index)
|
{
|
string newInputValue = string.Empty;
|
if ((InputCtrl.GetShowStyle(this.Items[index]) & numShowStyle.ThousandSeparator) == numShowStyle.ThousandSeparator)
|
{
|
newInputValue = CConvert.ToThousandSeparator(CConvert.ToDecimal(this.Items[index].Text));
|
}
|
else
|
{
|
newInputValue = GetItemValue(sender, index);
|
}
|
if (!newInputValue.Equals(this.Items[index].OrgText))
|
{
|
//lÏXµ½©Ç¤©
|
if (ValueChanged != null)
|
{
|
string saveText = GetItemValue(sender, index);
|
CTextBoxArrayBaseEventArgs vce = new CTextBoxArrayBaseEventArgs() { Cancel = false };
|
ValueChanged(this, index, vce);
|
if (vce.Cancel)
|
{
|
//³ÌlÉß·
|
if (vce.ResetOrgValue) { SetItemValue(sender, index, this.Items[index].OrgText); }
|
return false;
|
}
|
SetItemValue(sender, index, saveText);
|
}
|
}
|
return true;
|
}
|
|
|
private void SetInputValue(object sender, int index)
|
{
|
if ((InputCtrl.GetShowStyle(this.Items[index]) & numShowStyle.ThousandSeparator) == numShowStyle.ThousandSeparator)
|
{
|
this.Items[index].OrgText = CConvert.ToThousandSeparator(CConvert.ToDecimal(this.Items[index].Text));
|
}
|
else
|
{
|
this.Items[index].OrgText = GetItemValue(sender, index);
|
}
|
//System.Diagnostics.Debug.WriteLine(string.Format("SetInputValue({0},{1}) orgValue = {2}", (sender as Control).Name, index, orgValue));
|
}
|
|
|
private void Cell_KeyDown(object sender, KeyEventArgs e)
|
{
|
int index = items.IndexOf((sender as CTextBox));
|
if (KeyDown != null)
|
{
|
KeyDown(this, index, e);
|
}
|
else
|
{
|
switch (e.KeyCode)
|
{
|
case Keys.Down:
|
case Keys.Return:
|
if (m_ShiftEnterEnabled && e.KeyCode == Keys.Return && e.Shift)
|
{
|
Cell_KeyDown(sender, new KeyEventArgs(Keys.Up));
|
return;
|
}
|
|
if (e.Control == true && this.Items[index].Multiline)
|
{
|
isChangeLine = true;
|
return;
|
}
|
|
isChangeLine = false;
|
|
if (!this.Items[index].EnableUpDownKey && e.KeyCode == Keys.Up) { return; }
|
|
if (!m_ReadOnly)
|
{
|
if (!CheckValueChanged(sender, index)) { return; } //lÏXµ½çACxg¶
|
|
if (!InputCtrl.IsCorrectText(this.Items[index]))
|
{
|
(sender as Control).Focus();
|
return;
|
}
|
|
//»ÌO`FbN
|
if (IsCorrectEtc != null)
|
{
|
if (!IsCorrectEtc(this, index, new CTextBoxArrayBaseEventArgs() { CheckAll = false, ShowErrMsg = true }))
|
{
|
(sender as Control).Focus(); //µ½È¢ÍHH
|
return;
|
}
|
}
|
|
SetInputValue(sender, index);
|
}
|
if (NextFocus != null) { NextFocus(this, index, e); }
|
if (autoNextFocus) { SetAutoNextFocus(index, e); }
|
break;
|
case Keys.Up:
|
case Keys.Escape:
|
if (m_ShiftEnterEnabled && e.KeyCode == Keys.Up && this.Items[index].Multiline && !IsTopLine(this.Items[index])) { return; }
|
if (!this.Items[index].EnableUpDownKey && e.KeyCode == Keys.Down) { return; }
|
if (NextFocus != null) { NextFocus(this, index, e); }
|
if (autoNextFocus) { SetAutoNextFocus(index, e); }
|
break;
|
case Keys.End:
|
if (m_ReadOnly) { e.Handled = true; return; }
|
if ((InputCtrl.GetShowStyle(this.Items[index]) & numShowStyle.ShowList) != numShowStyle.ShowList) { return; }
|
if (KeyEnd != null)
|
{
|
e.Handled = true;
|
KeyEnd(this, index, e);
|
}
|
break;
|
}
|
}
|
}
|
|
private bool IsTopLine(CTextBox obj)
|
{
|
try
|
{
|
if (obj.SelectionStart == 0) { return true; }
|
|
string line = obj.Text.Substring(0, obj.SelectionStart);
|
return !line.Contains(Environment.NewLine);
|
}
|
catch
|
{
|
return true;
|
}
|
}
|
|
private void Cell_Validated(object sender, EventArgs e)
|
{
|
if (m_ReadOnly) { return; }
|
int index = items.IndexOf((sender as CTextBox));
|
if (Validated != null)
|
{
|
Validated(this, index, new CTextBoxArrayBaseEventArgs());
|
}
|
}
|
|
private void Cell_DoubleClick(object sender, EventArgs e)
|
{
|
if (m_ReadOnly) { return; }
|
CTextBox curField = (sender as CTextBox);
|
int index = items.IndexOf(curField);
|
if ((InputCtrl.GetShowStyle(curField) & numShowStyle.ShowList) == numShowStyle.ShowList)
|
{
|
//ê\¦
|
if (KeyEnd != null)
|
{
|
KeyEnd(this, index, new KeyEventArgs(Keys.End));
|
}
|
}
|
}
|
|
private void Cell_Enter(object sender, EventArgs e)
|
{
|
CTextBox curField = (sender as CTextBox);
|
System.Diagnostics.Debug.WriteLine("Cell_Enter " + curField.Name);
|
orgBkColor = curField.BackColor;
|
int index = items.IndexOf(curField);
|
acitveIndex = index;
|
if (Enter != null)
|
{
|
Enter(this, index, new CTextBoxArrayBaseEventArgs());
|
}
|
else
|
{
|
this.Items[index].OrgText = curField.Text;
|
System.Diagnostics.Debug.WriteLine("Cell_Enter " + curField.Name + " Org = " + this.Items[index].OrgText);
|
InputCtrl.TextEnter(curField);
|
if (AfterEnter != null) { AfterEnter(this, index, new CTextBoxArrayBaseEventArgs()); }
|
}
|
}
|
|
private void Cell_Leave(object sender, EventArgs e)
|
{
|
CTextBox curField = (sender as CTextBox);
|
System.Diagnostics.Debug.WriteLine("Cell_Leave " + curField.Name);
|
int index = items.IndexOf((sender as CTextBox));
|
if (Leave != null)
|
{
|
Leave(this, index, new CTextBoxArrayBaseEventArgs());
|
}
|
else
|
{
|
InputCtrl.TextLeave(curField);
|
if (curField.ReadOnly || m_UsingOrgBackColor) { curField.BackColor = orgBkColor; }
|
}
|
if (AfterLeave != null) { AfterLeave(this, index, new CTextBoxArrayBaseEventArgs()); }
|
}
|
|
private void Cell_KeyPress(object sender, KeyPressEventArgs e)
|
{
|
//3 'Ctrl-C Copy 22 'Ctrl-V Paste 24 'Ctrl-X Cut //¯ Redmine#8743
|
if (e.KeyChar == 3 || e.KeyChar == 22 || e.KeyChar == 24)
|
{
|
return;
|
}
|
|
CTextBox curField = (sender as CTextBox);
|
int index = items.IndexOf((sender as CTextBox));
|
if (KeyPress != null)
|
{
|
KeyPress(this, index, e);
|
}
|
else
|
{
|
if (curField.Multiline) { if (MultiKeyPress != null) { MultiKeyPress(this, index, e); } }
|
if (curField.IsChkData) { InputCtrl.TextBoxKeyPress(curField, e, curField.MaxLength, InputCtrl.GetInputType(curField)); }
|
if (!m_ForceChangeLine)
|
{
|
if (!isChangeLine && e.KeyChar == 13)
|
{
|
e.Handled = true;
|
}
|
}
|
isChangeLine = false;
|
}
|
}
|
|
private void Cell_TextChanged(object sender, EventArgs e)
|
{
|
if (m_ReadOnly) { return; }
|
if (disbleTextChanged) { return; }
|
int index = items.IndexOf((sender as CTextBox));
|
if (TextChanged != null)
|
{
|
TextChanged(this, index, new CTextBoxArrayBaseEventArgs());
|
}
|
}
|
|
private void Cell_Validating(object sender, CancelEventArgs e)
|
{
|
System.Diagnostics.Debug.WriteLine("Cell_Validating " + (sender as Control).Name);
|
if (m_ReadOnly) { return; }
|
if (this.disbleValidating) { return; }
|
int index = items.IndexOf((sender as CTextBox));
|
if (Validating != null)
|
{
|
Validating(this, index, e);
|
}
|
else
|
{
|
bool isCheck = false;
|
if (!this.Items[index].OrgText.Equals(this.Items[index].Text)) { isCheck = true; }
|
|
if (!isCheck)
|
{
|
if (ForceValidating == null)
|
{
|
return;
|
}
|
else
|
{
|
//l¯¶Åà`FbNðs¤
|
CTextBoxArrayBaseEventArgs forceValidatingEventArgs = new CTextBoxArrayBaseEventArgs() { Cancel = true };
|
ForceValidating(this, index, forceValidatingEventArgs);
|
if (forceValidatingEventArgs.Cancel) { return; }
|
}
|
}
|
|
bool isPass = false;
|
|
//System.Diagnostics.Debug.WriteLine("Org:" + orgValue);
|
if (!CheckValueChanged(sender, index)) { return; } //lÏXµ½çACxg¶
|
|
if (InputCtrl.IsCorrectText(this.Items[index], false))
|
{
|
if (IsCorrectEtc == null)
|
{
|
isPass = true;
|
}
|
else
|
{
|
if (IsCorrectEtc(this, index, new CTextBoxArrayBaseEventArgs() { CheckAll = false, ShowErrMsg = false }))
|
{
|
isPass = true;
|
}
|
}
|
}
|
|
if (isPass)
|
{
|
SetInputValue(sender, index);
|
}
|
else
|
{
|
SetItemValue(sender, index, this.Items[index].OrgText);
|
}
|
}
|
}
|
|
private bool SetAutoNextFocus(int index, KeyEventArgs e)
|
{
|
try
|
{
|
if (BeforeAutoNextFocus != null)
|
{
|
BeforeAutoNextFocus(this, index, e);
|
if (e.Handled) { return true; }
|
}
|
|
int hitIdx = -1;
|
if (e.KeyCode == Keys.Return)
|
{
|
for (int i = index + 1; i < items.Count; i++)
|
{
|
if (GeneralSub.CtrlCanFocus(items[i]))
|
{
|
hitIdx = i;
|
break;
|
}
|
}
|
|
if (hitIdx == -1) { return false; }
|
}
|
else if (e.KeyCode == Keys.Up)
|
{
|
if (index == 0) { return true; }
|
|
for (int i = index - 1; i >= 0; i--)
|
{
|
if (GeneralSub.CtrlCanFocus(items[i]))
|
{
|
hitIdx = i;
|
break;
|
}
|
}
|
|
if (hitIdx == -1) { return false; }
|
}
|
items[hitIdx].Focus();
|
e.Handled = true;
|
return true;
|
}
|
catch
|
{
|
return false;
|
}
|
finally
|
{
|
if (AfterAutoNextFocus != null) { AfterAutoNextFocus(this, index, e); }
|
}
|
}
|
|
private void InitAllCtrl(Control owner)
|
{
|
foreach (Control obj in owner.Controls)
|
{
|
if ((obj is ITextBoxArray) && (obj as ITextBoxArray).TextBoxArrayMember)
|
{
|
if (obj is CTextBox) { this.Add(obj as CTextBox); }
|
}
|
|
//ÌKw
|
if (obj.Controls.Count > 0) { InitAllCtrl(obj); }
|
}
|
}
|
|
/// <summary>
|
/// üÍÝèú»·é
|
/// </summary>
|
/// <param name="ownerForm"></param>
|
public void InitInputSetting(Control owner)
|
{
|
InitAllCtrl(owner);
|
items.Sort(itemsComparer);
|
}
|
}
|
}
|