-1

I am very new in C#. I have written a code to get two numbers in two text boxes and basically show their multiplication in a third text box.

The code is like:

private void button1_Click(object sender, EventArgs e)
{
    double A = double.Parse(textBox2.Text); 
    double B = double.Parse(textBox3.Text); //gets the hourly wage
    double C = A * B; 
}

I have written them all in an executing button class. How can I get "A" and "B" in their own private texbox classes and relate them in "C" text box class? I need to do it in order to validate the textboxes to give the user an error if he leaves any textboxes empty.

Zbigniew
  • 27,184
  • 6
  • 59
  • 66
user2326844
  • 321
  • 2
  • 8
  • 20
  • 4
    What you mean by `How can I get "A" and "B" in their own private texbox classes and relate them in "C" text box class?` – Zbigniew Apr 27 '13 at 13:17
  • I can't get the point either. What are you trying to accomplish? – Harits Fadillah Apr 27 '13 at 13:19
  • Ain't the A and B are already related to "their own private textboxes"?, As you are getting A and B from textBox2 and textBox3 – Khadim Ali Apr 27 '13 at 13:23
  • 4
    maybe we're not mature enough to understand :) – Darko Kenda Apr 27 '13 at 13:24
  • 1
    Probably this comment will get deleted, but I will go as far as to say that in you investigate what [MVVM](http://stackoverflow.com/questions/14381402/wpf-programming-methodology/14382137#14382137) is, you'll realize that winforms doesn't support "mature programming" and that you need more advanced and modern technologies for that. – Federico Berasategui Apr 27 '13 at 13:41

2 Answers2

0

You may restrict user to fill in the text boxes before executing the button logic in this way:

private void button1_Click(object sender, EventArgs e)
{
    if(textBox2.Text == string.Empty || textBox3.Text == string.Empty)
    {
        MessageBox.Show("Invalid input");
        return;
    }

    double A = double.Parse(textBox2.Text); 
    double B = double.Parse(textBox3.Text); //gets the hourly wage
    double C = A * B; 
}
Khadim Ali
  • 2,548
  • 3
  • 33
  • 61
  • Personally I'd rather use `if(string.IsNullOrWhiteSpace(textBox2.Text))`, so it would check whitespaces too. But, I'm not really sure if this is the answer for this question. – Zbigniew Apr 27 '13 at 13:38
  • @walkhard I just guessed it from the asker's statement "... I need to do it in order to validate the textboxes to give the user an error if he leaves any textboxes empty." – Khadim Ali Apr 27 '13 at 13:51
0

This is what u do to display your answer in the third textbox

private void button1_Click(object sender, EventArgs e)
{
    if(textBox2.Text == string.Empty || textBox3.Text == string.Empty)
    {
      MessageBox.Show("Please Fill Both Text Box");
      return;
    }

    double A = double.Parse(textBox2.Text); 
    double B = double.Parse(textBox3.Text); 
    textbox4.Text = (A * B).ToString(); 
}
Raz Mahato
  • 875
  • 1
  • 7
  • 11