string str="";
private void textBox31_KeyDown(object sender, KeyEventArgs e)
{
int KeyCode = e.KeyValue;
if (!IsNumeric(KeyCode))
{
e.Handled = true;
return;
}
else
{
e.Handled = true;
}
if (((KeyCode == 8) || (KeyCode == 46)) && (str.Length > 0))
{
str = str.Substring(0, str.Length - 1);
}
else if (!((KeyCode == 8) || (KeyCode == 46)))
{
str = str + Convert.ToChar(KeyCode);
}
if (str.Length == 0)
{
textBox31.Text = "0.00";
}
if (str.Length == 1)
{
textBox31.Text = "0.0" + str;
}
else if (str.Length == 2)
{
textBox31.Text = "0." + str;
}
else if (str.Length > 2)
{
textBox31.Text = str.Substring(0, str.Length - 2) + "." + str.Substring(str.Length - 2);
}
}
private bool IsNumeric(int Val)
{
return ((Val >= 48 && Val <= 57) || (Val == 8) || (Val == 46));
}
private void textBox31_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
I have used the above code for numeric precsion upto two.the problem is when i'm pressing number pad 0 to number 9 then number is not taking ..if i pressed the numbers above the qwerty keys in the keyboard then its taking.
please let me know where is the problem?
Loading
Benjamin KemnerPosted Aug 4, 2011, 10:08 AM
insert it after " int KeyCode = e.KeyValue;"
switch(e.KeyCode){
case Keys.NumPad1:
KeyCode = 49;
break;
case Keys.NumPad2:
KeyCode = 50;
break;
case Keys.NumPad3:
KeyCode = 51;
break;
case Keys.NumPad4:
KeyCode = 52;
break;
case Keys.NumPad5:
KeyCode = 53;
break;
case Keys.NumPad6:
KeyCode = 54;
break;
case Keys.NumPad7:
KeyCode = 55;
break;
case Keys.NumPad8:
KeyCode = 56;
break;
case Keys.NumPad9:
KeyCode = 57;
break;
case Keys.NumPad0:
KeyCode = 48;
break;
}
saif begPosted Aug 4, 2011, 10:19 AM
Thank you sir...thanks a lot
VulpesPosted Aug 4, 2011, 10:08 AM
saif begPosted Aug 4, 2011, 9:55 AM
so the problem still exist
Benjamin KemnerPosted Aug 4, 2011, 9:44 AM
1234567890
on qwertz:
49,50,51,52,53,54,55,56,57,48
on numpad:
97,98,99,100,101,102,103,104,105