Send values from one form to another form

2018-12-31 02:44发布

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, Form2 should open and Form1 should be in inactive mode (i.e not selectable).

Form2 contains one text box and one submit button. When I type any message in Form2's text box and click the submit button, the Form2 should close and Form1 should highlight with the submitted value.

How can i do it? Can somebody help me to do this with a simple example.

19条回答
浮光初槿花落
2楼-- · 2018-12-31 03:30

There are several solutions to this but this is the pattern I tend to use.

// Form 1
// inside the button click event
using(Form2 form2 = new Form2()) 
{
    if(form2.ShowDialog() == DialogResult.OK) 
    {
        someControlOnForm1.Text = form2.TheValue;
    }
}

And...

// Inside Form2
// Create a public property to serve the value
public string TheValue 
{
    get { return someTextBoxOnForm2.Text; }
}
查看更多
若你有天会懂
3楼-- · 2018-12-31 03:30

declare string in form1 public string TextBoxString;

in form1 click event add

private void button1_Click(object sender, EventArgs e)
    {
        Form1 newform = new Form1();
        newform = this;
        this.Hide();
        MySecform = new Form2(ref newform);
        MySecform.Show();
    }

in form2 constructer

public Form2(ref Form1 form1handel)
    {
        firstformRef = form1handel;
        InitializeComponent();
    }

in form2 crate variable Form1 firstformRef;

private void Submitt_Click(object sender, EventArgs e)
    {
        firstformRef.TextBoxString = textBox1.Text;
        this.Close();
        firstformRef.Show();

    }
查看更多
残风、尘缘若梦
4楼-- · 2018-12-31 03:32

Ok so Form1 has a textbox, first of all you have to set this Form1 textbox to public in textbox property.

Code Form1:

Public button1_click()
{
    Form2 secondForm = new Form2(this);
    secondForm.Show();
}

Pass Form1 as this in the constructor.

Code Form2:

Private Form1 _firstForm;

Public Form2(Form1 firstForm)
{
    _firstForm = firstForm:
}

Public button_click()
{
    _firstForm.textBox.text=label1.text;
    This.Close();
}
查看更多
人气声优
5楼-- · 2018-12-31 03:32

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

查看更多
泛滥B
6楼-- · 2018-12-31 03:35

Declare a public string in form1

public string getdata;

In button of form1

form2 frm= new form2();
this.hide();
form2.show();

To send data to form1 you can try any event and code following in that event

form1 frm= new form1();
form1.getdata="some string to be sent to form1";

Now after closing of form2 and opening of form1, you can use returned data in getdata string.

查看更多
与风俱净
7楼-- · 2018-12-31 03:37

if you change Modifiers Property of a control in a Form to Public, another Forms can access to that control. f.e. :

    Form2 frm;
    private void Form1_Load(object sender, EventArgs e)
    {
        frm = new Form2();
        frm.Show();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(frm.txtUserName.Text); 
        //txtUserName is a TextBox with Modifiers=Public        
    }
查看更多
登录 后发表回答