Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to use multiple form. the first form shall serve as the main form while the other forms would carry individual functions. A simple example, I would like to calculate BMI. thus in the main form when the user inserts their height and weight. The data should be passed to second form and calculation of bmi shall take place there. once the calculation is done the answer would be shown in main form. so far i could only send the data from a to another form. when retrieving back the data I'm not getting the calculated data. I always get back my original label text value.
ps ~ I created the example above just to make my question clearer.

In my case Label1.Text value always takes the initial value in SecondForm.Label1.Text rather than taking the value after the function radio.button1.click is done.

Here is the actual code I'm using:
VB
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim SecondForm As New Form2
        SecondForm.Show()
        SecondForm.RadioButton1.PerformClick()

        If SecondForm.RadioButton1.Checked = True Then
        Label1.Text = SecondForm.Label1.Text

    End If
    End Sub
Posted
Updated 8-Jul-14 17:31pm
v2

 
Share this answer
 
Comments
Member 10915260 8-Jul-14 23:51pm    
Thanks for the link. Struggling to digest it as I'm not used to C# format. Anyways I'm using online converter to get a clearer picture.
Create a delegate and an event in your second form, then subscribe to the event in the main form.

In second form class:
C#
public delegate void UpdateCalculation(object sender, double value);
public event UpdateCalculation OnUpdateCalculation;


Actually this is the preferred way
C#
public class CalculationEventArgs : EventArgs
{
  CalculationEventArgs(double _value)
  {
    CalculatedValue = _value;
  } 

  double CalculatedValue { get; set; }
}
public delegate void UpdateCalculation(object sender, CalculationEventArgs e);
public event UpdateCalculation OnUpdateCalculation;


In some method in your second form
C#
void SomeMethod()
{
  if (OnUpdateCalculation != null)
    OnUpdateCalculation(this, new CalculationEventArgs(25.0));
}


In main form:
C#
Form2 frm2 = new Form2();
frm2.OnUpdateCalculation += OnUpdateCalculation_fired;
 
Share this answer
 
v2
 
Share this answer
 
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
 

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