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:19

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.

查看更多
回忆,回不去的记忆
3楼-- · 2018-12-31 03:21

After a series of struggle for passing the data from one form to another i finally found a stable answer. It works like charm.

All you need to do is declare a variable as public static datatype 'variableName' in one form and assign the value to this variable which you want to pass to another form and call this variable in another form using directly the form name (Don't create object of this form as static variables can be accessed directly) and access this variable value.

Example of such is,

Form1

public static int quantity;
quantity=TextBox1.text; \\Value which you want to pass

Form2

TextBox2.Text=Form1.quantity;\\ Data will be placed in TextBox2
查看更多
何处买醉
4楼-- · 2018-12-31 03:23

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

查看更多
冷夜・残月
5楼-- · 2018-12-31 03:28

Define a property

public static class ControlID {  
    public static string TextData { get; set; }
}

In the form2

private void button1_Click(object sender, EventArgs e)
{  
    ControlID.TextData = txtTextData.Text;   
}

Getting the data in form1 and form3

private void button1_Click(object sender, EventArgs e)
{   
    string text=  ControlID.TextData;   
}
查看更多
深知你不懂我心
6楼-- · 2018-12-31 03:28

In this code, you pass a text to Form2. Form2 shows that text in textBox1. User types new text into textBox1 and presses the submit button. Form1 grabs that text and shows it in a textbox on Form1.

public class Form2 : Form
{
    private string oldText;

    public Form2(string newText):this()
    {
        oldText = newText;
        btnSubmit.DialogResult = DialogResult.OK;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        textBox1.Text = oldText;
    }

    public string getText()
    {
        return textBox1.Text;
    }

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}

And this is Form1 code:

public class Form1:Form
{
    using (Form2 dialogForm = new Form2("old text to show in Form2"))
    {
        DialogResult dr = dialogForm.ShowDialog(this);
        if (dr == DialogResult.OK)
        {
            tbSubmittedText = dialogForm.getText();
        }
        dialogForm.Close();
    }
}
查看更多
只靠听说
7楼-- · 2018-12-31 03:28

you can pass as parameter the textbox of the Form1, like this:

On Form 1 buttom handler:

private void button2_Click(object sender, EventArgs e)
{
Form2 newWindow = new Form2(textBoxForReturnValue);
newWindow.Show(); 
}

On the Form 2

public static TextBox textBox2; // class atribute

public Form2(TextBox textBoxForReturnValue)
{
    textBox2= textBoxForReturnValue;
}

private void btnClose_Click(object sender, EventArgs e)
{

    textBox2.Text = dataGridView1.CurrentCell.Value.ToString().Trim();
    this.Close();
}
查看更多
登录 后发表回答