Tuesday, July 28, 2009

C#.Net Question?

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)


{


if (double.Parse(textBox1.Text) %26lt; double.Parse(textBox2.Text))


{


MessageBox.Show("mali");


}


else


{


me = double.Parse(textBox1.Text) - double.Parse(textBox2.Text);


}


}





Why do I get an "input string format error" when I enter a number on textbox2.text?





please do note that variable "me" is declared as double.





Thanks in advance!

C#.Net Question?
Your event is for a keypress, and when you enter a number, you trigger that event before both textboxes have a value, so it tries to convert a blank string to a double value, and errors. You'll have to catch that error with something like





if(textBox1.Text != "" %26amp;%26amp; textBox2.Text != "")





"I forgot to include that before you can type on textbox2 you have to fill in textbox1 with a number first... which makes textbox1 != Null"





Ok, then your issue is that the keyPress event triggers before the value enters into the textbox. You want to trigger the event when the value is changed, not when a key is pressed.


No comments:

Post a Comment