简体   繁体   中英

Text from 1 form to another form in C#

I'm using . I have 2 forms which contain a TextBox and a Button in each form. My goal is to get the content from the TextBox on form1 to the TextBox on form2 on a click of a button. I also want to close form1 and only show form2 with the content that was typed in the TextBox on form1 .

This is what I have so far. The problem with my code is the content doesn't show in form2 .

this.Hide();

Form2 f2 = new Form2();
f2.ShowDialog();

f2.textBox1.Text = textBox1.Text;           

this.Close();
//close form1

Given that the code you provided is from your button click event. It can be simply modified as follows to get what you need.

Form2 f2 = new Form2();
f2.textBox1.Text = textBox1.Text;  
f2.Show();

this.Close();

However if your Form1 is the main form, ie it is loaded from Program.cs. Your application will just exit after this.Close()

I also advise that you don't make Form2's textBox1. Keep the property private and create a function to modify its value, for example: Create this function in Form2

public void SetTextBox1Text(string text)
{
    textBox1.Text = text;
}

And your Form1 button click event:

Form2 f2 = new Form2();
f2.SetTextBox1Text(textBox1.Text);  
f2.Show();

this.Close();

First in your Form2.cs add a property to expose your private textBox1 as a good practice.

public partial class Form2 : Form
{
    //this is a property
    public string TextBox1
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }
    public Form2()
    {
        InitializeComponent();
    }
}

Next, in your Form1.cs, assign the value of textBox1 to Form2 first before showing the dialog.

    private void button1_Click(object sender, EventArgs e)
    {
        this.Hide();
        Form2 f2 = new Form2();
        //assign the value first before showing the dialog
        f2.TextBox1 = textBox1.Text;
        f2.ShowDialog();

        this.Close();
    }

ShowDialog() displays a modal window of Form2 which means you cannot go to the parent form which is Form1

f2.textBox1.Text = textBox1.Text;           
this.Close();

The above two lines are written after f2.ShowDialog(); which will execute only when Form2 is closed. Hence you are not getting any text in the TextBox in Form2 .

And coming to the part where you are sending the Form1 TextBox value to Form2 , the shortest and easiest way is to pass the Form1 TextBox value as string to the constructor of Form2 .

Following is the code which

Form1 Button Click:

private void button1_Click(object sender, EventArgs e)
{
    this.Hide();
    Form2 f2 = new Form2(textBox1.Text);
    f2.ShowDialog();           
} 

Form 2 constructor:

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

Hope this helps.

The problem lies here:

f2.ShowDialog();

The method ShowDialog() is a blocking call. The code you have after this will simply not be executed until f2 is closed. Try replacing it with Show() .

Edit: as pointed out by @Abhishek, using Show() doesn't solve the problem since you want to close the first form after the second one is done. The problem is still the same though, ShowDialog() will wait there for a DialogResult and therefor will not execute the next statement. Consider setting the textbox content before showing the form.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM