passing data between 3 windows forms in visual studio using C#

后端 未结 5 1824
无人及你
无人及你 2021-01-27 03:23

I have a windows application which has 3 forms : Form1,2,3. I want to send text of a textbox from form2 to form1 and then that same text from for

5条回答
  •  春和景丽
    2021-01-27 03:51

    I would recommend a change from using constructors to using properties. This will keep things properly 'contained' and it's pretty simple.

    EX:

    public partial class Form3 : Form
    {
       public String form1Text {get; set;}
    
       public Form3()
       {
           InitializeComponent();
       }
    }
    

    public partial class Form2 : Form
    {
       public Form2()
       {
           InitializeComponent();
           string loginname = form2_textbox.Text;
       }
    
       public String form2Text {get; set;}
    
       private void send_to_form1_button_Click(object sender, EventArgs e)
       {
    
           form2Text = form2_textbox.Text;
           this.DialogResult = DialogResult.Ok;
           this.Close();
       }
    }
    

    Then in form 1:

    public partial class Form1 : Form
    {
       string receivefromForm2a;
    
       public Form1()
       { InitializeComponent(); }       
    
        private void openform3_Click(object sender, EventArgs e)
        {
            Form3 f3 = new Form3();
            f3.form1Text = receivefromForm2a;
            f3.Show();
        }
    
    private void OPENFORM2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        if(f2.ShowDialog() == DialogResult.Ok)
        {
          receivefromForm2a = f2.form2Text; //New Property on Form2.
        }
    }
    }
    

提交回复
热议问题