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. Only if I open a new instance of Form1, on Form2 it works. Why this happen? Can t I send the text to the old instance?

I m using this code;

Form1 (Main Form)

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

Form2 SecondaryForm = new Form2();

SecondaryForm.ShowDialog();

Form2 (Second Form with user data)

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?

最佳回答

What you can do is pass the reference of MainForm(Form1) to second Form(Form2). Then instead of creating MainForm again use the reference to update the 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()
问题回答

This is because you re creating a instance of Form1 in your Form2 code. What you want to do is setup Form2 s parentForm to be the instance of the Form1 that created it.

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.

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
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签