Dear friends!
I have a button in form1, and now I want when clicking on it, a new form (form2) will appear. There's a text box on this form2. User will input a integer here. After user's pressing Enter, this integer will be given to a function into Form1.cs and disappeared. I've tried some ways to get the number but unsuccessfully. Any suggestion?
Thanks in advanced!
mario285 is offline Add to mario285's Reputation Report Post Reply With Quote

Recommended Answers

All 3 Replies

Hi svatstika and welcome to DaniWeb :)

The easiest way to do this is to provide a reference to the input in your forms like so:

public partial class Form1: Form
{
   // create a property to store the input from the user
   public string UserInput
   {
      get;
      set;
   }

   // event handler for when the button is clicked - open the new form and wait until the OK dialog result is received
   private void myButton_click(object sender, EventArgs e)
   {
      UserInput = String.Empty;
      Form2 newForm = new Form2();
      if (newForm.ShowDialog() == DialogResult.OK)
        UserInput = newForm.Input;
   }
}

public partial class Form2: Form
{
   // a property that can be used by Form1 to retrieve the input from the user
   public string Input
   {
     get { return myTextBox.Text; }
     set { myTextBox.Text = value; }
   }
}
commented: Nice. +8

Hi again! I'm a newbie so I don't understand following block:
public string Input
{
get { return myTextBox.Text; }
set { myTextBox.Text = value; }
}
I couldn't find anything similar to this in my ebooks or in the internet.
I hope you give me an explanation.
You're great. Thanks for your helpl!

This is an example of a class Property. What it is doing is providing a way to retrieve (get) and store (set) a string called Input, which in this case points directly to the Text property of a text box called myTextBox. The following code is equivalent (but a lot more typing!):

private string _input;
public string RetrieveInput()
{
   return _input;
}
public void StoreInput(string value)
{
   _input = value;
}
// event handler to handle when the text changes in the text box
private void myTextBox_TextChanged(object sender, EventArgs e)
{
   StoreInput(myTextBox.Text);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.