简体   繁体   English

在不打开新实例的情况下将数据返回到窗体

[英]Returning data to Forms without opening a new instance

I am trying to return some data from Form2 to Form1, everything seems fine, I got the data and so, but, when I try to pass my data to a textbox, it doesn't changes the text. 我试图将一些数据从Form2返回到Form1,一切似乎很好,我得到了数据,因此,但是,当我尝试将数据传递到文本框时,它不会更改文本。 Only if I open a new instance of Form1, on Form2 it works. 仅当我打开Form1的新实例时,在Form2上它才有效。 Why this happen? 为什么会这样? Can't I send the text to the old instance? 我不能将文本发送到旧实例吗?

I'm using this code; 我正在使用此代码;

Form1 (Main Form) Form1(主窗体)

public void updateText(string data)
{
    MessageBox.Show(data);
    txtGood.Text = data;
}

Form2 SecondaryForm = new Form2();

SecondaryForm.ShowDialog();

Form2 (Second Form with user data) Form2(具有用户数据的第二个表单)

Form1 MainForm = new Form1();
MainForm.updateText(data);
MainForm.ShowDialog();
this.Close();

So, my question is, how can I pass the data values to the old instance of the main form? 因此,我的问题是,如何将数据值传递给主表单的旧实例? without having to create a new instance and show a new instance. 无需创建新实例并显示新实例。 Is there a way to do this? 有没有办法做到这一点?

This is because you're creating a instance of Form1 in your Form2 code. 这是因为您正在Form2代码中创建Form1的实例。 What you want to do is setup Form2's parentForm to be the instance of the Form1 that created it. 您要做的是将Form2的parentForm设置为创建它的Form1的实例。

public partial class Form1 : Form
{
    public void CreateForm2()
    {
        Form2 form2 = new Form2(this);
        form2.ShowDialog();
    }

    public string MyTextboxText
    {
        get { return txtMyTextbox.Text; }
        set { txtMyTextbox.Text = value; }
    }
}

public partial class Form2 : Form
{
    private Form1 parentForm;

    public Form2(Form1 parentForm)
    {
        this.parentForm = parentForm;
    }

    public void myButtonClick() 
    {
        parentForm.MyTextboxText = "Hello";
    }
}

This code is just an example, probably wont compile as-is. 这段代码只是一个示例,可能无法原样编译。

What you can do is pass the reference of MainForm(Form1) to second Form(Form2) . 您可以做的是将MainForm(Form1)的引用传递给second Form(Form2) Then instead of creating MainForm again use the reference to update the textbox . 然后,不用重新创建MainForm ,而使用引用来更新textbox

   //pass reference to form2
   Form2 SecondaryForm = new Form2(mainForm);
   SecondaryForm.ShowDialog();

    //in the constructor of Form2 save the reference of Form1
    Form1 form1 = null

    Form2(Form1 mainForm)
    {
        form1 = mainForm;
    }

    //then instead of creating a new MainForm again just use reference of Form1

    form1.updateText(data);
    this.Close()

main form: 主要形式:

private void Button2_Click(object sender, EventArgs e) {
    frmCustomersRecord rec = new frmCustomersRecord(this); 
    rec.ShowDialog();
    rec.GetData(); 
}

child form: 子表格:

public partial class frmCustomersRecord : Form 
{
    public frmCustomersRecord()
    {
        //blank contructor (Instance of an class)
    }

    frmCustomerDetails cd;

    public frmCustomersRecord(frmCustomerDetails parentForm) : this()
    {
        this.cd = parentForm; 
    } 
    //call the methods using child form object
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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