Many new programmer face a problem to checking whether textbox input is numeric or not. So you can easily implement this code on your windows form. Call this function on textbox1_Keypress event and it will prevent to enter non-numeric character in textbox.This code only accept integer value and works in Window Application.
<code><span>public static bool CheckNumber(char ch)
{
if (Convert.ToInt32(ch) == 8 )
return false;
if(char.IsDigit(ch)==false)
return true;
return false;
}</span></code>
Call this function as follows
<code>private void txtBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = CheckNumber(e.KeyChar, ((TextBox)sender).Text);
}</code>
<code><span>public static bool CheckNumber(char ch)
{
if (Convert.ToInt32(ch) == 8 )
return false;
if(char.IsDigit(ch)==false)
return true;
return false;
}</span></code>
Call this function as follows
<code>private void txtBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = CheckNumber(e.KeyChar, ((TextBox)sender).Text);
}</code>
Comments
Post a Comment