سلام
من میخوام TextBox را فقط به اعداد اختصاص بدم یعنی وقتی حروف زدم وارد نشه.یا برعکس فقط به حروف اختصاص بدم یعنی وقتی اعداد بزم وارد نشه.
سلام
من میخوام TextBox را فقط به اعداد اختصاص بدم یعنی وقتی حروف زدم وارد نشه.یا برعکس فقط به حروف اختصاص بدم یعنی وقتی اعداد بزم وارد نشه.
ممنونم ولی این که با vb بود
اگه کسی با C# بلده از ما بی دریغ نکنه.
ممنون
// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;
// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
// Initialize the flag to false.
nonNumberEntered = false;
// Determine whether the keystroke is a number from the top of the keyboard.
if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
{
// Determine whether the keystroke is a number from the keypad.
if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
{
// Determine whether the keystroke is a backspace.
if (e.KeyCode != Keys.Back)
{
// A non-numerical keystroke was pressed.
// Set the flag to true and evaluate in KeyPress event.
nonNumberEntered = true;
}
}
}
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (nonNumberEntered == true)
{
MessageBox.Show("Please enter number only...");
e.Handled = true;
}
}
در رویداد KeyPerss Text box اینا رو بنویس
اگر میخوای عدد وارد بشه
e.Handled = (char.IsDigit(e.KeyChar) || char.IsControl(e.KeyChar)) ? false : true;
اگه میخوای حروف وارد بشه
e.Handled = (char.IsLetter(e.KeyChar) || char.IsControl(e.KeyChar) || char.IsWhiteSpace(e.KeyChar)) ? false : true;
کد زیر رو توی event -> keyPress اون textbox بنویسین که فقط عدد رو قبول کنه :
if ((!char.IsDigit(e.KeyChar)) && (((int)e.KeyChar) != 8))
{
e.Handled = true;
return;
}
8 کذ اسکی BackSpace هست که اگر می خواین اون هم غیر فعال بشه از شرط حدفش کنین.
از كنترل MaskTextBoX هم ميتوني استفاده كني با استفاده از خاصيت ماسك كنترل
e.Handled = true;
این خط کد چیه
شما کلمه Handled رو به عنوان کاری که قراره انجام بشه در نظر بگیر ( تایپ شدن متن در TextBox )، حالا با این ذهنیت، این خط کد براتون قابل درک میشه!
وقتی True باشه کار مورد نظر انجام میشه، و اگر False باشه انجام نمیشه :)