New to windows app in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pbj2009
    New Member
    • Jul 2009
    • 5

    New to windows app in C#

    Hello all. I am used to using Write and WriteLine when trying to display my results using the COnsole.
    Now working with windows applications, I am trying to display the results in a textbox but keep coming up with the wrong thing.
    It should say:

    The sum of [integer entered by the user in txtfirst textbox] and [integer entered by user in the txtsecond textbox] is: [displays answer here in txtanswer]

    What it DOES say is:
    The sum of System.Windows. Form.

    I don't understand why I am getting that.
    Here is the code for btnadd. Can someone give me some guidance please?

    Code:
    private void btnadd_Click(object sender, System.EventArgs e)
            {
                
                string inValue;
                string inValue2;
                double firstNum;
                double secondNum;
                double add;
    
                inValue = txtfirst.Text;
                inValue2 = txtsecond.Text;
    
                firstNum = double.Parse(inValue);
                secondNum = double.Parse(inValue2);
                add = (firstNum + secondNum);
                txtanswer.Text = string.Format("The sum of {0} and {1} is: {2}", txtfirst, txtsecond, add).ToString();
            }
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You are really close.

    Code:
    txtanswer.Text = string.Format("The sum of {0} and {1} is: {2}", txtfirst, txtsecond, add).ToString();
    {0} is being formatted with textfirst (a control) not with txtfirst.text, the text of the control. If the textbox were wider you would see the rest of what it is trying to say:
    "The sum of System.Windows. Forms.Textbox and System.Windows. Forms.Text is 42", or whatever the right sum is.


    same issue with txtsecond

    You can also take out the ending ".ToString( );
    string.Format returns a string, you don't have to turn a string into a string.

    Comment

    • pbj2009
      New Member
      • Jul 2009
      • 5

      #3
      Ah ok. I understand now. Thanks so much. :)

      Comment

      • deadOTMOPO3
        New Member
        • Jul 2009
        • 2

        #4
        I think it should be something like:
        Code:
        label1.Text = "The sum of " + textBox1.Text + " and " + textBox2.Text + " is:" + add.ToString();

        Comment

        • vipin sharma
          New Member
          • Jul 2009
          • 16

          #5
          Originally posted by pbj2009
          Code:
          txtanswer.Text = string.Format("The sum of {0} and {1} is: {2}", txtfirst, txtsecond, add).ToString();
                  }
          write txtfirst.Text, txtsecond.Text in string.Format() ;

          Reason for you error is that txtfirst is a text box not the text in that textbox.

          Comment

          Working...