Passing parameters back and forth between forms in C #

I am creating an application in which the user will press a button on form1, which will cause the display of form2. Then the user will fill in the chat in form2, and then click the button that closes form2 and send back the parameters of form1 for processing. How to do it in C #? I have seen people using properties for this, but the examples are not clear enough. Can someone give some sample code showing me how I can pass parameters? I would prefer a property method, but as long as it works, I will consider this an answer.

+3
source share
2 answers

Simply put, put the form elements in the second form, as usual. You can then add public accessories to this form, which you can extract from links and links. For example, if Form2 has text fields that you would like to back off, you could:

class Form2
{
  // Form2.designer.cs
  private TextBox TextBox1;

  // Form2.cs
  public String TextBoxValue // retrieving a value from
  {
    get
    {
      return this.TextBox1.Text;
    }
  }

  public Form2(String InitialTextBoxValue) // passing value to
  {
    IntiializeComponent();

    this.TextBox1.Text = InitialTextBoxValue;
  }
}

Then just access it later when you create the form (same as OpenFileDialog for Filename, etc.

public void Button1_Click(object sender, EventArgs e)
{
  Form2 form2 = new Form2("Bob");      // Start with "Bob"
  form2.ShowDialog();                  // Dialog opens and user enters "John" and closes it
  MessageBox.Show(form2.TextBoxValue); // now the value is "John"
}

The same can be done for Int32, Boolean, etc. It just depends on the value of the form if you want to make it / confirm or otherwise.

, Modifiers , , . , , ( , , / Form2)

+10

:

, Form1 Form2 :

Form1 and Form2

"txtBoxForm1" Form1 Form2, "txtBoxForm2" Form2. , Form2 , [Return to Form1], "txtBoxForm2" , ( ) "txtBoxForm1" Form1.

:

1) Form1 Form2, ShowDialog():

 public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private void btnSend_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();

        //Step 1) 
        //Display the form passing parameter(s) via overloading 
        //the ShowDialog() method. 
        //In this example the parameter is the 'txtBoxForm1' on Form1.
        // f2.ShowDialog(); is replaced by
        f2.ShowDialog(ref txtBoxForm1); 

    }
}

'txtBoxForm1', . , Form2, Form1 , Form2.

Form1, , , f2.ShowDialog().

2) () ShowDialog():

public partial class Form2 : Form
{

    public Form2()
    {
        InitializeComponent();
    }

    private void btnReturn_Click(object sender, EventArgs e)
    {
        this.Close(); 
    }

    //Step 2)
    //Receiving and returning parameter(s) via the overloaded ShowDialog() method.
    //This saves the need to have Properties and or fields associated
    //to parameters when overloading the above Form() constructor instead.
    public void ShowDialog(ref TextBox txtBoxForm1)
    {
        //Assign received parameter(s) to local context
        txtBoxForm2.Text = txtBoxForm1.Text;

        this.ShowDialog(); //Display and activate this form (Form2)

        //Return parameter(s)
        txtBoxForm1.Text = txtBoxForm2.Text;
    }
}

(Form2 ), , . . , ShowDialog(), Form2() Form1, . Form2() ShowDialog() Form2. , '(ref txtBoxForm1) , .

( ) #.net. ShowDialog(), , Framework ShowDialog().net 'this.ShowDialog();' .

.

, !

+4

Source: https://habr.com/ru/post/1785649/


All Articles