Question:
sharing variables between forms (VB.net)?
Ulver
2007-03-27 15:01:26 UTC
If I have 2 forms, a main form (which has a label box that shows the calculation done on the second form) and the second form (used to input date and then calculate it). How do I make it so the calculation from the second form will show up in the label in the first form?
Four answers:
Kevin M
2007-03-27 15:50:18 UTC
Here's how I do it. Make a new component class, we'll call it class1. In the class declaration, declare a variable, we'll call it result1.



public class class1 : System.ComponentModel.Component

{

public static string result1;

}



Now on your second form, set result1 =

So,



class1.result1 = ;



Now back on your first form, just set the label's text as normal, but use our class's result1 variable.



Label.Text = class1.result1;





Good luck ;p
Jeff R
2007-03-28 20:29:46 UTC
Use an event in the second form. Here is an example. I've created a project with two forms Form1 and Form2. Form1 will be my main form and has a button named Button1 and a label named Label1. Form2 will have a DateTimePicker control. Here is the source code.



'FORM2 Code

Public Class Form1

Private WithEvents mfForm2 As Form2





Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

mfForm2 = New Form2

AddHandler mfForm2.CalculationDone, AddressOf mfForm2_CalculationDone

mfForm2.Show()

End Sub



Private Sub mfForm2_CalculationDone(ByVal vnCalculatedValue As Integer)

Label1.Text = vnCalculatedValue.ToString

End Sub

End Class



'FORM2 Code

Public Class Form2

Public Event CalculationDone(ByVal vnCalculatedValue As Integer)





Private Sub DateTimePicker1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker1.ValueChanged

Dim nDays As Integer = DateDiff(DateInterval.Day, Now, DateTimePicker1.Value)

RaiseEvent CalculationDone(nDays)

End Sub

End Class



What we have done here is added an event CalculationDone to form2. This event is raised anytime the user changes the date value in the datetime picker control. It passes the number of days into the future (or past as a negative number) via the event.



Form1 has a button control that shows form2 and then an event handler that captures the CalculationDone event and posts the result in the label.
Gene
2007-03-27 16:35:15 UTC
Another approach is to create a property on the second form and set the value of that property equal to the result of your calculation. Then you can reference the exposed property of the second form from the first form before the second form is closed. The second form's property would need to have a scope that enabled you to see it from the first form (like Public, Friend, etc.)
anonymous
2007-03-28 13:50:28 UTC
Yah, what they said. Another way (I think is the easiest), is to make a module. Then declare a variable (Public Test as String). Then that string can be shared between both forms. I dont think Kevin is showing VB.Net though...maybe C, C#, C++, or JAVA?


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...