C#中設置textbox限制前提的辦法。本站提示廣大學習愛好者:(C#中設置textbox限制前提的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中設置textbox限制前提的辦法正文
本文實例講述了C#中設置textbox限制前提的辦法,分享給年夜家供年夜家參考。詳細完成辦法以下:
#region 設置數目等textbox控件款式及限制前提(詳細挪用的辦法就是重寫或直接挪用ShieldNumberTextBoxOtherKeys函數)
/// <summary>
/// 屏障數字textbox的其他字符串
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public virtual void tBoxNumbers_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
switch (e.KeyCode)
{
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
case Keys.NumPad0:
case Keys.NumPad1:
case Keys.NumPad2:
case Keys.NumPad3:
case Keys.NumPad4:
case Keys.NumPad5:
case Keys.NumPad6:
case Keys.NumPad7:
case Keys.NumPad8:
case Keys.NumPad9:
case Keys.Back:
case Keys.OemPeriod:
case Keys.Delete:
case Keys.Decimal:
e.SuppressKeyPress = false;
break;
default:
break;
}
}
public virtual void tBoxNumbers_KeyPress(object sender, KeyPressEventArgs e)
{
TextBox tBox = sender as TextBox;
char c = e.KeyChar;
if (c.ToString().Equals("."))
{
if (tBox.Text.Length <= 0)
e.Handled = true; //小數點不克不及在第一名
else
{
float f;
float oldf;
bool b1 = false, b2 = false;
b1 = float.TryParse(tBox.Text, out oldf);
b2 = float.TryParse(tBox.Text + e.KeyChar.ToString(), out f);
if (b2 == false)
{
if (b1 == true)
e.Handled = true;
else
e.Handled = false;
}
}
}
}
/// <summary>
/// 屏障數字textbox的其他字符串
/// </summary>
/// <param name="tbox">要屏障的textbox</param>
public virtual void ShieldNumberTextBoxOtherKeys(TextBox tbox)
{
tbox.ImeMode = ImeMode.Disable;
tbox.KeyDown += tBoxNumbers_KeyDown;
tbox.KeyPress += tBoxNumbers_KeyPress;
}
#endregion
願望本文所述對年夜家的C#法式設計有所贊助。