How to carry a value from one form to another form in vb.net windows applications?
Web applications contains facilities to transfer the value from one form to another using server objects like Session, Application. Same way in windows applications we can use global variables able to solve this problem. For ex., In your windows application you want to display welcome “user name” after users logged in application, how can? Please follows below one.
In computer programming, a global variable is a variable that is accessible in every scope. They are contrasted with local variables
please go wikipedia for more..
In vb.net application you should declare the global variables in modules,but c# calss itself,first add module.vb file in your application using solution explorer.
Public strCustomerID As String ‘variable declared with public keyword
In our application have two forms first one is login form(frmLogin.vb) and another one mainform(frmMain.vb),our criteria show the username in mainform after logged in.show first assign the login form(frmLogin.vb) user name textbox value into global variable strCustomerID.
Assign the value to variable
frmLogin.vb login button event you can get the user name.
Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
Display the Value
As per our requirement “How to display the username in main form(frmMain.vb)In frmMain.vb form load event you can able to get the username.
Me.Label1.Text = “Welcome ” & strCustomerID
After using this variable,you should clear this object memory using nothing keyword.
strCustomerID = nothing(object is free)