G
Guest
I have a windows from which I want to react only when the user presses one of
the digits keys (0-9). And every digit key should enter the string "str"
variable. If the user presses the enter key I want the program to make some
additional oparations and when the backspace is pressed I want the last digit
to be removed from the string str.
Unfortunately this doesn't work.
Here is the code:
private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==Convert.ToChar(Keys.Decimal) &&
e.KeyChar!=Convert.ToChar(Keys.Back))
str=str+e.KeyChar.ToString();
if(e.KeyChar==Convert.ToChar(Keys.Back))
str.Remove(str.Length-1,1);
if(e.KeyChar==Convert.ToChar(Keys.Enter))
{
if(Convert.ToDecimal(str)==y*x)
{
MessageBox.Show("Correct");
StopTime();
ChangeStatus();
}
else
MessageBox.Show("WRONG!!!");
}
}
I used the Convert class because it would say it cannot convert a char to a
Keys in the if statements
What should I do?
the digits keys (0-9). And every digit key should enter the string "str"
variable. If the user presses the enter key I want the program to make some
additional oparations and when the backspace is pressed I want the last digit
to be removed from the string str.
Unfortunately this doesn't work.
Here is the code:
private void Form1_KeyPress(object sender,
System.Windows.Forms.KeyPressEventArgs e)
{
if(e.KeyChar==Convert.ToChar(Keys.Decimal) &&
e.KeyChar!=Convert.ToChar(Keys.Back))
str=str+e.KeyChar.ToString();
if(e.KeyChar==Convert.ToChar(Keys.Back))
str.Remove(str.Length-1,1);
if(e.KeyChar==Convert.ToChar(Keys.Enter))
{
if(Convert.ToDecimal(str)==y*x)
{
MessageBox.Show("Correct");
StopTime();
ChangeStatus();
}
else
MessageBox.Show("WRONG!!!");
}
}
I used the Convert class because it would say it cannot convert a char to a
Keys in the if statements
What should I do?