General Doubt

nikki

Member
Joined
Sep 7, 2006
Messages
21
Programming Experience
Beginner
I Want To Declare A Value To Variable In One Form.that Value Should Be Displayed In Another Form.suppose I Declare

Dim A As Integer
A=10

The Value 10 Should Be Displayed In Another Form.

Is It Possible?
 
You will need an instance of your other form. Then you can pass in any value you like, via the constructor, properties, methods etc....

The property way would go...


Inside form2


VB.NET:
Private MyVariable as Integer = 0
 
Friend Poperty SetMyVariable as Integer
Get
Return Me.MyVaraible
End Get
Set (ByVal Value as Integer)
Me.Myvariable = value
End Set 
End Property


Inside Form1

VB.NET:
Dim NewForm2 as New Form2
NewForm2.SetMyVaraible = 10


The property inside form2 now holds the value of 10, or any varaible you want to set it to.
 
Another way would be to use global variables.

I have a module in my project called ModVariables, and in there I define all my variables I want to be accessed across the whole instance.

I.E in your example I would have

VB.NET:
Module modVariables
 
Friend A as Integer
 
End Module

On form 1 you could have a textbox and a button, and in the textbox type what you want to store...in this case 10
On the Button_onClick event you would use the code;

VB.NET:
[SIZE=2][COLOR=#0000ff]rivate[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.EventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] button1.Click[/SIZE]
[SIZE=2] 
[/SIZE]A = textbox1.text
 
End Sub

Then on form2 another textbox and button and onclick event of the button you can say something like me.textbox2.text = A

Obviously you can hardcode the variables into code, or have them as parameters by someone entering and saving like shown above.
Form 2 you could have on form Load code to display the variable as well....the choices are endless!

Hope that helps.
Luke
 
Back
Top