none
how to read a number from a textbox from another form

    Question

  • I've a Form1 where I use a textbox to read a number and I've a button to call Form2. I want to use the number from Form1 in Form2, but I don't get it.

    In Form1, I have:

    x=System::Double::Parse(textBox1->Text);
    Form2 ^about = gcnew Form2;
    about->ShowDialog(this);
    In Form2 I have for example:
    y=sin(2*pi*x);
    Thanks for any information you have.
    Friday, October 23, 2009 10:49 PM

Answers

  • Create a public method in Form2 to accept the number.
    It can store that in a private variable for later use.

    Before calling the ShowDialog function call the newly created public method to pass in the number.
    x=System::Double::Parse(textBox1->Text);
    Form2 ^about = gcnew Form2;
    about->SetNumber(x);
    about->ShowDialog(this);


    «_Superman_»
    Microsoft MVP (Visual C++)
    • Marked as answer by kzona Friday, October 23, 2009 11:22 PM
    Friday, October 23, 2009 11:03 PM
  • class Form2
    {
         private double x;
    
         public void SetNumber(double d)
         {
            x = d;
         }
    }
    My managed code syntax could be wrong.
    But this is the general idea.


    «_Superman_»
    Microsoft MVP (Visual C++)
    • Marked as answer by kzona Friday, October 23, 2009 11:22 PM
    Friday, October 23, 2009 11:19 PM

All replies

  • Create a public method in Form2 to accept the number.
    It can store that in a private variable for later use.

    Before calling the ShowDialog function call the newly created public method to pass in the number.
    x=System::Double::Parse(textBox1->Text);
    Form2 ^about = gcnew Form2;
    about->SetNumber(x);
    about->ShowDialog(this);


    «_Superman_»
    Microsoft MVP (Visual C++)
    • Marked as answer by kzona Friday, October 23, 2009 11:22 PM
    Friday, October 23, 2009 11:03 PM
  • Could you give an example of a public method to accept the number?

    I'm getting the follow error:
    error C2039: 'SetNumber' : is not a member of 'test::Form2'

    thank you so much
    Friday, October 23, 2009 11:17 PM
  • class Form2
    {
         private double x;
    
         public void SetNumber(double d)
         {
            x = d;
         }
    }
    My managed code syntax could be wrong.
    But this is the general idea.


    «_Superman_»
    Microsoft MVP (Visual C++)
    • Marked as answer by kzona Friday, October 23, 2009 11:22 PM
    Friday, October 23, 2009 11:19 PM
  • Thank you.
    You'll save me.
    I'll try it.
    Friday, October 23, 2009 11:23 PM