از Inheritance استفاده کنید چطور؟
یه کلاس جدید درست کنید و از TextBox اون رو مشتق کنید و دستورات رو اونجا بنویسید.حالا پروژه رو Compile کنید و به جای اینکه تکست باک رو به فرم بندازید از اون کلاس نوشته شده یه دونه به فرم بندازید.
این جوری هر تغییری در کد اصلی بدید تغییرات خود به خود ثبت نیشه.
اینم نمونه کدش که من واقعا باهاش زیاد کار کردم و راضیم.شما می تونید بعضی event هاش رو پاک کنید.
class txt:TextBox
{
bool _Number = false;
char[] _Chars;
bool _Seprator = false;
public char[] Chars
{
get
{
return _Chars;
}
set
{
_Chars = value;
}
}
public Boolean Number
{
get
{
return _Number;
}
set
{
_Number = value;
}
}
public bool Seprator
{
get
{
return _Seprator;
}
set
{
_Seprator = value;
}
}
protected override void OnTextChanged(EventArgs e)
{
string strHelper = Text;
try
{
if (Seprator && (!string.IsNullOrEmpty(strHelper)))
{
if (Convert.ToDecimal(strHelper) >= 0)
{
strHelper = string.Format("{0:N0}", Int64.Parse(strHelper.Replace(",", "")));
//Select(Text.Length, 0);
}
else//mines
{
strHelper = strHelper.Remove(0, 1);
strHelper = string.Format("{0:N0}", Int64.Parse(strHelper.Replace(",", "")));
strHelper = "-" + strHelper;
//Select(Text.Length, 0);
}
}
Text = strHelper;
Select(Text.Length, 0);
}
catch
{
}
finally
{
base.OnTextChanged(e);
}
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
// base.OnKeyPress(e);
Boolean handle= true;
if (e.KeyChar == (char)Keys.Return)
e.Handled = true;
else if (char.IsControl(e.KeyChar))
e.Handled = false;
else if (e.KeyChar == (char)Keys.Return)
e.Handled = true;
else
{
if (Number)
e.Handled = !(Char.IsDigit(e.KeyChar));
else
{
if (Chars != null)
{
foreach (char ch in Chars)
{
if (e.KeyChar == ch)
handle = false;
}
e.Handled = handle;
}
}
}
}
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter)
{
SendKeys.Send("{TAB}");
}
return base.ProcessDialogKey(keyData);
}
protected override void OnGotFocus(EventArgs e)
{
this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
base.OnGotFocus(e);
}
protected override void OnLostFocus(EventArgs e)
{
if (this.ReadOnly)
this.BackColor = System.Drawing.SystemColors.ButtonFace;
else
this.BackColor = System.Drawing.Color.White;
base.OnLostFocus(e);
}
private void InitializeComponent()
{
this.SuspendLayout();
this.ResumeLayout(false);
}
}