简体   繁体   中英

simple passing value from one form to another

I'm trying to pass value from one form to another in winforms.

On my main form I have btnAddNewRecord and dataOptions combobox.

User should first select from combobox(dataOptions) and than click on btnAddNewRecord. I want to pass this user selected value from dataoptions combobox to new form, so I tried like this

MainForm

private void btnAddNewRecord_Click(object sender, EventArgs e)
{
   var formAddRecord = new FormNewRecord();
   formAddRecord.ShowDialog();
 }
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
     IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
     var formAddRecord = new FormNewRecord();
     formAddRecord.SelectedDataOptions = data.ToString();        
}

FormNewRecord.cs

public string SelectedDataOptions {get; set;}
private void FormNewRecord_Load(,,,,,)
{
   txtSelectedDataOptions.Text = SelectedDataOptions;
}

no error on build, but on debugging txtSelectedDataOptions is not populated with passed value. What I'm doing wrong here?

Thanks

You are creating two different instances of FormNewRecord . Make formAddRecord as private field and show it on button click.

FormNewRecord formAddRecord = new FormNewRecord();

private void btnAddNewRecord_Click(object sender, EventArgs e)
{
     formAddRecord.ShowDialog();
}

private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
     IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
     formAddRecord.SelectedDataOptions = data.ToString();
}

Well, formAddRecord should be a private field of your class, not a var redeclared in each method !

(Method btnAddNewRecord_Click has no ideas of variables declared in Method dataOptions_SelectedIndexChanged , by the way you create different instances).

So

private FormNewRecord formNewRecord_ = new FormNewRecord();


private void btnAddNewRecord_Click(object sender, EventArgs e)
{
   formNewRecord_ .ShowDialog();
 }
private void Form1_Load()
{ populating combobox...}
private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
{
     IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
     formNewRecord_.SelectedDataOptions = data.ToString();        
}

I don't think new a form instance in another form is a good way, a better way you can set the data you want to pass as public in the parent form, and when you show the child form, set the parent form as child's owner, then you can get and use the data in the child form.

  1. set the data as a public property in the parent form, like this:
    Main Form:

    public string passData = ""; 
    private void btnAddNewRecord_Click(object sender, EventArgs e)
    {
        var formAddRecord = new FormNewRecord();
        formAddRecord.ShowDialog(this); //important
    }
    private void Form1_Load()
    { populating combobox...}
    private void dataOptions_SelectedIndexChanged(object sender, EventArgs e)
    {
        IMyCustomData data = (IMyCustomData)dataOptions.SelectedItem;
        passData = data.ToString();   //store the selected value to passData
    }

2.get passed data from child's owner:

FormNewRecord.cs

private void FormNewRecord_Load(,,,,,)
{
   if(this.Owner != null)
   {
       MainForm mf = (MainForm)this.Owner;
       txtSelectedDataOptions.Text = mf.passData;
   }
}

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