I've been working on this project for about an hour now and I got stuck. I've got 4 forms but only the last 3 is relevant. In form 2 I use:

    this.Visible = false;
    Form3 Form3 = new Form3();
    Form3.Show();

To create and show form 3. Form3 also got a textbox which is empty and I want to transfer that info to a label in Form4. In form 3 I use the same cod as in Form 2 to make Form 3.

I've tried a couple of things and searched on the forums but nothing seems to work...

     lblN2.Text = Form3.txtf.Text;

I want to transfer the text that the user writes in the textbox(txtf) in Form3 to a empty label(lblN2) in Form4.

有帮助吗?

解决方案

Try something like this (code in Form3 class):

    Form4 frm4 = new Form4();
    frm4.lblN2.Text = this.txtf.Text;
frm4.Show();

Alternative would be to modify constructor method in Form4 to accept string parameter and invoke it as follows:

 Form4 frm4 = new Form4(this.txtf.Text);
    frm4.Show();

其他提示

Based on your information, I think you want to send the value of form2 to form3, You can modify constructor (as a solution) to send the form2 value to form3. Here is a sample.

For form 2:

this.Visible = false;
Form3 frm = new Form3(value-you-want-to-send);
frm.Show();

in Form3 you should have constructor taking a argument to get value from Form2 AS:

public void Form3(value-you-want-to-receive)
{
  //set the label text to the string received
}

You should probably specify Form4 in your statement, like:

Form4 _frm4 = new Form4();
_frm4.lblN2.Text = Form3.txtf.Text

On Form 3...

    private void button1_Click(object sender, EventArgs e)
    {
        Form4 frm = new Form4(textBox1.Text);
        frm.Show();
    }

On Form 4...

public partial class Form4 : Form
{
    private string _valueFromOtherForm;
    public Form4()
    {
        InitializeComponent();
    }

    public Form4(string valuePassed)
    {
        InitializeComponent();
        _valueFromOtherForm = valuePassed;
    }

    private void Form4_Load(object sender, EventArgs e)
    {
        label1.Text = _valueFromOtherForm;
    }
}

}

In Form4, write a method like:

public void ReceiveTextFromAnotherForm(string theText)
{
  //set the label text to the string received
}

In Form3, do this:

Form4 theForm4 = new Form4();
theForm4.ReceiveTextFromAnotherForm(this.txtf.Text);
theForm4.Show();

If you want to transfer something to form4 then you can create a public variable on form4, then you can do something like this on form3:

this.hide();
form4 form4 = new form4();
form4.variable = textbox1.text;
form4.show();

then on form4_load you can:

textbox2.text = variable;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top