[RESOLVED][02/03] how to pass variable from one form to another form
Hi all,
Now i'm facing a problem when i passing a variable from one form to another form, i can't get the value.
I used the method of property.
vb code
Form A
Code:
Private UserID as string
Private Sub New()
Me.User_ID = textbox1.text
End Sub
Public property User_ID() as string
Get
Return UserID
End Get
Set (ByVal Value As String)
UserID = Value
End Set
end property
Form B --> Try to get value from Variable form A
Code:
dim frm as new FormA
textbox1.text = frm.User_ID
Re: [02/03] how to pass variable from one form to another form
Re: [02/03] how to pass variable from one form to another form
Thx Andy showed me the link.
I faced the problem while passing value from parent to child .
Re: [02/03] how to pass variable from one form to another form
Assign a variable as public in Form A and then assign some values to that variable and call that variable from Form B.
Example
Public test as string="Testing"
'Write this code in Form A and write the following line in Form B
messagebox.show(FormA.test)
Re: [02/03] how to pass variable from one form to another form
You had it backwards in the constructor. It should be
Code:
Private Sub New()
Me.textbox1.text = UserID
End Sub
And in formA, when you instantiate formB, you should set the User_ID property of formB
Code:
'This is done in formA
Dim frmB As New FormB
frmB.User_ID = "whatever id you want to put in here"
frmB.Show()
Re: [02/03] how to pass variable from one form to another form
Thx stanax,
I ever tried your method, but when everytime i need passing variables i have to create a lot of property.Furthermore, if i create a property inside a class why i cannot used the class public property to store the value when program is running ?
Stanax i rated you ... thanks a lot
Re: [02/03]Please Help how to pass variable from one form to another form
My project contains a class store the public variable
In VB 6 i always used
Code:
Public Variable as string
to assign a variable to store value along project is running.
In .net i'm still new
I tried create a class but still cannot obtains the value
Class
Code:
Public Class VariableStorage
Private UserID As String
Public Sub New()
MyBase.new()
End Sub
Public Property User_ID() As String
Get
Return UserID
End Get
Set(ByVal Value As String)
UserID = Value
End Set
End Property
End Class
Code:
Private Sub cmdLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLogin.Click
Dim USerID As New VariableStorage
USerID.User_ID = Me.txtUserID.Text.ToString
end sub
Code:
Private Sub mdiForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim UserID As New VariableStorage
Me.sbpMdiForm.Panels.Item(0).Text = "User ID: " & UserID.User_ID
END SUB
thanks !!!!!
Re: [02/03] how to pass variable from one form to another form
Quote:
Originally Posted by nUflAvOrS
Thx stanax,
I ever tried your method, but when everytime i need passing variables i have to create a lot of property.Furthermore, if i create a property inside a class why i cannot used the class public property to store the value when program is running ?
Stanax i rated you ... thanks a lot
Unless it is absolutely needed, I tend not to use public variables because they're considered as bad programming practice... Why is it bad? If you just google for "why are public variables bad?" and you'll get the image :)
Re: [RESOLVED][02/03] how to pass variable from one form to another form
THANKS stanav ,, ;-) i will take ur advise