0
votes

I created 2 forms (form1 and form2). In form1, press f5 function key it will display form 2. Then I want pass the value to form1 and just close the form2 when button click fire.

Form1 Code

public string fo            
{
    get { return txtCusID.Text; }
    set { txtCusID.Text = value; }
}

Form 2 code

public partial class SearchForm : Form  
{   
   PawningForm f2 =new PawningForm();

   private void button1_Click_1(object sender, EventArgs e)
   {
       lblTest.Text = row.Cells[0].Value.ToString();
       f2.fo = lblTest.Text;
       f2.Show();
   }
}

but when I click button on from2 it will create form1 twice, because "f2.Show();" and existing form1. But I want just pass value to existing form1.

1
When in the first form code you open the second form, pass the instance of the first form and it instead of creating a new instance of the first formSteve

1 Answers

0
votes

When you create the instance of the SearchForm from the calling form (PawningForm) you could pass the instance of the calling form and keep it in a global variable of the SearchForm, so you code could be changed simply with these lines.

Form 1 code

SearchForm f = SearchForm(this);
f.ShowDialog();

Form 2 code

public partial class SearchForm : Form
{
     PawningForm f2;
     public void SearchForm(PawningForm caller) 
     {
          f2 = caller;
          InitializeComponent();
     }
     private void button1_Click_1(object sender, EventArgs e)
     {
        lblTest.Text = row.Cells[0].Value.ToString();
        f2.fo = lblTest.Text;
     }

  }

Now, when you click the button, the instance represented by the variable f2 is not a new instance, but the one that has called the SearchForm instance