Multiply without a button
SO I have three text boxes in my application, is it possible ot multply the numbers in the first two boxes in the third one without the user pressing a button?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
IQPosted Jul 7, 2009, 11:46 AM
Jon BellamyPosted Jul 7, 2009, 4:48 AM
Yes you can do that under the textbox_LostFocus() event. Lets assume your textboxes are TextBox1, TextBox2, and TextBox3 with the value being displayed in TextBox3. Firstly set TextBox3.IsEnabled property to false and then add a new event to the TextBox2_LostFocus property and code as following.
private void textBox2_LostFocus(object sender, RoutedEventArgs e)
{
multiplyValues();
}
private void multiplyValues()
{
if (textbox1.Text != String.Empty && textBox2.Text != String.Empty)
{
try
{
textBox3.Text = (Convert.ToDouble(textBox1.Text) * Convert.ToDouble(textBox2.Text).ToString();
}
catch (Exception)
{
MessageBox.Show("Both Values must be numeric","Error", MessageboxButton.OK, MessageBoxImage.Error);
}
}
}
By enclosing the code you could also place a call t the multipleValues() method in the lost focus event of TextBox1 if you wished to update the value everytime one of the values were changed - the above has very simple error capturing, but covers probably most of your needs.
Hope that helps - Jon