2

I'm creating a tool to manage WLAN networks in Windows 8.1 (because W8 doesn't have one with a GUI). In the application

Screenshot of the application

Clicking on Add button shows another form. After I fill the fields, I want to click on "OK", close the second form, and handle the input data on the first form.

I tried to implement the examples in this question Send values from one form the another form, but couldn't. (I'm a newbie, and they're not really clear.)

Can someone provide a working example?


First form "Add" button

private void AddButton_Click(object sender, EventArgs e)
{
    // show second form

    // get input values (upon clicking on "OK" and closing the second form)

    // handle them
}

Second form "OK" button

private void OKButton_Click(object sender, EventArgs e)
{
    // send input values to first form

    this.Close();
}
Community
  • 1
  • 1
akinuri
  • 10,690
  • 10
  • 65
  • 102
  • you can pass data by an Object! Create a public object in child form and when closing the second form save any data on that object. then in parent form (first form) read data from object that stored second form data. – Behzad Jul 04 '15 at 05:01
  • possible duplicate of [Communicate between two windows forms in C#](http://stackoverflow.com/questions/1665533/communicate-between-two-windows-forms-in-c-sharp) – Siamak Ferdos Jul 04 '15 at 07:45

4 Answers4

3

first you should create a class like:

public class YourFavoriteDefinedClass 
{
    public string NetworkName;
    public string SecurityType;
    public string EncryptType;
    public string SecurityKey;
}

then You can solve it by below procedures:

Procedure 1 : Add RefreshPerentList Method to Parent Form

you can declare a method in your Parent Form, then call it in OKButton_Click of your Second form to Refresh or add Item to your ListView.


Example:

first you should add below cod to your AddButton_Click method.

private void AddButton_Click(object sender, EventArgs e)
{
     var frmSecond = new YourSecondFormName();
     frmSecond.Owner = this;
     frmSecond.ShowDialoge();
}

then declare below method in Your ParentForm (Wireless Network Manager)

public void RefreshPerentList(YourFavoriteDefinedClass objSecondFormParams)
{
    // Implement Your Code Here to refresh or add item to listview.
    var strNetWorkName = objSecondFormParams.NetworkName;
    var stSecurtiyType = objSecondFormParams.SecurityType; 
    ...
}

You Can Pass SecondForm parameters using an object with special type or Class that I named for example : var objSecondFormParams = new YourFavoriteDefinedClass();

then You can call it in your OKButton_Click with below code:

private void OKButton_Click(object sender, EventArgs e)
{
     objSecondFormParams.NetworkName = txtNetWorkName.Text;
     objSecondFormParams.SecurityType= cbSecurityType.SelectedValue;
     ...

    ((YourParentFormName)this.Owner).RefreshPerentList(objSecondFormParams);

    this.Close();
}

Procedure 2 : Add Event In Second Form

you can also add an event in your second form then call its listener in Parent form to refresh or add item to your listview.

Nima Rostami
  • 2,652
  • 3
  • 15
  • 23
  • Used the first method you mentioned and it works fine. However I didn't understand this `(Manager)this.Owner).RefreshProfiles();` line. `Namespace.FormName.RefreshProfiles()` doesn't work. `FormName form1 = new FormName(); form1.RefreshProfiles()` doesn't work, but only this `(Manager)this.Owner).RefreshProfiles();`. I want to know more about this. – akinuri Jul 04 '15 at 08:31
  • @akinuri this line is to cast your second From's Owner to your First Form (Your Parent Form) that you set in first line before in order to Achieve its `RefreshPerentList` method. I set the `Owner` before using `frmSecond.Owner = this;` – Nima Rostami Jul 04 '15 at 09:36
1

first you should declare a delegate in form 1 ex:
public delegate void delPassData(TextBox text);

In form1’s button click event handler, instantiate form2 class and delegate.
Assign a function in form2 to the delegate and call the delegate as below:

private void btnSend_Click(object sender, System.EventArgs e)
{
    Form2 frm= new Form2();
    delPassData del=new delPassData(frm.funData);
    del(this.textBox1);
    frm.Show();
}

In form2, add a function to which the delegate should point to. This function will assign textbox’s text to the label:

public void funData(TextBox txtForm1)
{
     label1.Text = txtForm1.Text;
}
Behzad
  • 3,502
  • 4
  • 36
  • 63
1

You can pass data by an Object! Create a public object in child form and when closing the second form save any data on that object.
Then in parent form (first form) read data from that object where stored second form data.

First create a class match by your second form data:

public class mySecondFormData
{
    public string NetworkName;
    public string SecurityType;
    public string EncryptType;
    public string SecurityKey;
}

First form "Add" button:

private void AddButton_Click(object sender, EventArgs e)
{
    // show second form
    frm2.ShowDialog()
    // get input values (upon clicking on "OK" and closing the second form)
    var NetworkName = frm2.myData.NetworkName;
    var SecurityType = frm2.myData.SecurityType;
    ...
    // handle them
}

Second form:

public mySecondFormData myData = new mySecondFormData();

private void OKButton_Click(object sender, EventArgs e)
{
    // send input values to first form
    myData.NetworkName = txtNetworkName.text;
    myData.SecurityType = txtSecurityType.text;
    ...  

    this.Close();
}
Behzad
  • 3,502
  • 4
  • 36
  • 63
0

You can create properties in second form similar to this one.

private string network_name;
public string Network_Name
{
    get
    {
        return this.network_name;
    }
    set
    {
        this.network_name= value;
    }
}

and so on for each field.

Then you can easily access those from First form with some kind of RefreshMethod

RefreshMethod() on First form which will be executed on Second Form OK Click.

public void RefreshGrid()
{
    //get properties variable of second form
    //code to refresh list/grid
}

Finally close the second form.

Hemal
  • 3,682
  • 1
  • 23
  • 54