Send values from one form to another form

后端 未结 19 2488
眼角桃花
眼角桃花 2020-11-21 11:45

I want to pass values between two Forms (c#). How can I do it?

I have two forms: Form1 and Form2.

Form1 contains one button. When I click on that button, Fo

相关标签:
19条回答
  • 2020-11-21 11:56

    I've worked on various winform projects and as the applications gets more complex (more dialogs and interactions between them) then i've started to use some eventing system to help me out, because management of opening and closing windows manually will be hard to maintain and develope further.

    I've used CAB for my applications, it has an eventing system but it might be an overkill in your case :) You could write your own events for simpler applications

    0 讨论(0)
  • 2020-11-21 12:01

    You can make use of a different approach if you like.

    1. Using System.Action (Here you simply pass the main forms function as the parameter to the child form like a callback function)
    2. OpenForms Method ( You directly call one of your open forms)

    Using System.Action

    You can think of it as a callback function passed to the child form.

    // -------- IN THE MAIN FORM --------
    
    // CALLING THE CHILD FORM IN YOUR CODE LOOKS LIKE THIS
    Options frmOptions = new Options(UpdateSettings);
    frmOptions.Show();
    
    // YOUR FUNCTION IN THE MAIN FORM TO BE EXECUTED
    public void UpdateSettings(string data)
    {
       // DO YOUR STUFF HERE
    }
    
    // -------- IN THE CHILD FORM --------
    
    Action<string> UpdateSettings = null;
    
    // IN THE CHILD FORMS CONSTRUCTOR
    public Options(Action<string> UpdateSettings)
    {
        InitializeComponent();
        this.UpdateSettings = UpdateSettings;
    }
    
    private void btnUpdate_Click(object sender, EventArgs e)
    {
        // CALLING THE CALLBACK FUNCTION
        if (UpdateSettings != null)
            UpdateSettings("some data");
    }
    

    OpenForms Method

    This method is easy (2 lines). But only works with forms that are open. All you need to do is add these two lines where ever you want to pass some data.

    Main frmMain = (Main)Application.OpenForms["Main"];
    frmMain.UpdateSettings("Some data");
    

    I provided my answer to a similar question here

    0 讨论(0)
  • 2020-11-21 12:02

    Constructors are the best ways to pass data between forms or Gui Objects you can do this. In the form1 click button you should have:

    Form1.Enable = false;
    Form2 f = new Form2();
    f.ShowDialog();
    

    In form 2, when the user clicks the button it should have a code like this or similar:

    this.Close();
    Form1 form = new Form1(textBox1.Text)
    form.Show();
    

    Once inside the form load of form 1 you can add code to do anything as you get the values from constructor.

    0 讨论(0)
  • 2020-11-21 12:03

    Form1 Code :

    private void button1_Click(object sender, EventArgs e)
    {
                Form2 f2 = new Form2();
                f2.ShowDialog();
                MessageBox.Show("Form1 Message :"+Form2.t.Text); //can put label also in form 1 to show the value got from form2
    }
    

    Form2 Code :

            public Form2()
            {
                InitializeComponent();
                t = textBox1;                        //Initialize with static textbox
            }
            public static TextBox t=new TextBox();   //make static to get the same value as inserted
            private void button1_Click(object sender, EventArgs e)
            {
    
                this.Close();
    
            }
    

    It Works!

    0 讨论(0)
  • 2020-11-21 12:04

    How to pass the values from form to another form

    1.) Goto Form2 then Double click it. At the code type this.

    public Form2(string v)
    {
      InitializeComponent();
      textBox1.Text = v;
    }
    

    2.) Goto Form1 then Double click it. At the code type this. //At your command button in Form1

    private void button1_Click(object sender, EventArgs e)
    {
       Form2 F2 = new Form2(textBox1.Text);
       F2.Show();
    }
    
    0 讨论(0)
  • 2020-11-21 12:05

    How about using a public Event

    I would do it like this.

    public class Form2
    {
       public event Action<string> SomethingCompleted;
    
       private void Submit_Click(object sender, EventArgs e)
       {
           SomethingCompleted?.Invoke(txtData.Text);
           this.Close();
       }
    }
    

    and call it from Form1 like this.

    private void btnOpenForm2_Click(object sender, EventArgs e)
    {
        using (var frm = new Form2())
        {
            frm.SomethingCompleted += text => {
                this.txtData.Text = text;
            };
    
            frm.ShowDialog();
        }
    }
    

    Then, Form1 could get a text from Form2 when Form2 is closed

    Thank you.

    0 讨论(0)
提交回复
热议问题