Click here to Skip to main content
15,881,820 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello ,
C#
private void button1_Click(object sender, System.EventArgs e)
{
        WiFi_Fast_Setup_Form1 fs = new WiFi_Fast_Setup_Form1();
           // I need to pass NetworkName_tB.Text to the fs Form, and back again, 
           // once it is modified by the user.
        fs.Show();
}
Posted

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
Comments
JimOr 17-Mar-14 7:45am    
Thank you Sergey, Today I will investigate all the possibilities you have provided.
I will attempt to make a C# test application to impliment your recommendations
Thanks Kindly,
Jim
Hi,

In your case try like this.
In your form button click
C#
 WiFi_Fast_Setup_Form1 objfrm= new WiFi_Fast_Setup_Form1(textbox1.Text);
if (objfrm.ShowDialog() == DialogResult.OK)
           {
textbox1.Text=objfrm.newModifiedtext;
}



In WifiFast_Setup_form1 place on Ok and cancel button
C#
public String newModifiedtext= "";
      public  WiFi_Fast_Setup_Form1(String txtvalues)
      {
          InitializeComponent();
          newModifiedtext= txtvalues;
                  
      }

//In Ok button Click

  private void btnOK_Click(object sender, EventArgs e)
        {
            newModifiedtext="Now adding new text for test";
        }
 
Share this answer
 
Comments
JimOr 16-Mar-14 22:49pm    
Thank you for the response, I tried to impliment what you suggested,
it looks like you are pointing me in the right direction,
but I seem to be missing something.
Do you have a complete simple code project for this sample so I could run it and see how it works?
Thank You Very Kindly,
Jim
syed shanu 16-Mar-14 22:56pm    
this is very simple one as i tld u create main form and in that button click call another form but in that for you need to pass argumet as contructor to that form and in popup form init method you can get the arument data and store that result in public variable just do same like above code it will work.In your pop up form place 2 button as Ok and cancel .in cancel button no need to write anything and in Ok button write same like above it will solve ur problem.there also another method as you can create a pulic class and declare static public variable you can access that class public variable in any of your forms with in your project.
syed shanu 16-Mar-14 22:58pm    
if you cont do that send me your mail id i will send you simple code program.
JimOr 16-Mar-14 23:23pm    
Syed,
Thank you for your suggestion, I will try to get it working as you say.
Will try and sent comment tomorrow.
Kindly,
Jim
syed shanu 17-Mar-14 0:38am    
The Simplay will be as create a Static Class and declare all your globalvariable there,and access the public variable in all you class .
public static class pubClass
{

public static string pubTxtValues
{
get;
set;
}


}

private void button1_Click(object sender, EventArgs e)
{
frmpopup objfrm = new frmpopup(textBox1.Text);
pubClass.pubTxtValues = textBox2.Text;
if (objfrm.ShowDialog() == DialogResult.OK)
{
textBox1.Text = objfrm.newModifiedtext;


}
textBox2.Text = pubClass.pubTxtValues;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900