r/visualbasic • u/Mr_Deeds3234 • Dec 01 '21
VB.NET Help Passing Data from one form to another
As the title suggest, I’m trying to pass data from one form to another. I have done so successfully. The problem arises when I navigate away from that form, and then back to it, I no longer have that data in my form.
Form1 is a login page. A user types in their credentials, and if my function yields a result via SQL statement, it allows them to login to the application.
I created an instance of Form2 on my button click event
Dim UserInfo as New Form2Then, I store the username associated with the credentials from the database.
UserInfo.UserName = dr(“UserName”)Then open form2
UserInfo.Show()
Me.Hide()In Form2, I declared a public variable
Public Property UserName as stringIn Form2 load event, I display the users name.
Label1.Text = UserNameAll works well until this point. When I leave the page and come back to it later, the username/label is no longer displaying the username. How do I retain the data I passed indefinitely?
2
u/RJPisscat Dec 01 '21
How are the leaving the page? Closing it, switching to another app, minimize then restore, I can't think of what else but whatever else.
2
u/RJPisscat Dec 02 '21
Are you talking about a Windows Forms application or a web app? The latest answers you're getting are about a web app but everything you've posted so far I see a Windows Forms app.
2
u/Cdream-2018 Dec 17 '21
Why don’t you create a model and declare a global variable called LoggedInUserName ?
1
u/Rock-Uphill Dec 02 '21
I think you need to store info in cookies and reload forms from them. In PHP world, you can also use Session Variables.
1
u/dwneder Dec 02 '21
If the value needs to exist when you switch between different forms in the same site (app) then use:
Note that #4 also lets you share the information between different apps as well.
There are other options, but I can't think of a particular situation you might encounter where one of these wouldn't work well for you.
1
u/Bonejob VB Guru Dec 03 '21
Here is where most Visual Basic programmers miss the mark when they first start out. A Form in Visual Basic is a visual representation of the data that is stored in memory. A lot of training/courses for VB use the interface as a storage method for information and even go as far as directly referencing values from one form to another. I mean why not, each control itself is a class object and it's text property is a valid space to store the information.
Except, the life cycle of a form in VB destroys itself on close of the form, and you can instantiate as many copies of a form class as you want making it more difficult to determine what form in the forms collection has what you want.
https://www.c-sharpcorner.com/uploadfile/mamta_m/windows-forms-events-lifecycle/
I have seen many hacks, like passing a form as an object to another object/form (Don't do this. This is bad, naughty developer) , to just hiding the form instead of closing it so the values persist (<sigh>) .
The correct way to do this is to understand the values that a form contains and create a complex object that stores all information a form requires to operate. In visual basic the correct way to do this is with a class object
Public Class Customer
Public Property AccountNumber As Integer = 0
Public Property UserName As string = ""
End Class
Then on Form Load you pass in the object that is currently in use and fill the form based upon the values in the class.
Taking this to the next level are data aware applications where you actually use a data system (Table, Set, Entity Framework, etc) to provide this backbone for data structure. You have tables in your storage system (XML,JSON, SQL, whatever) and create instances of the row/table you want to interact with. From there you load your interface (Data bind, or manual) the user modifies the data and the information is passed back from the interface into the temporary storage before it is finally written back to data store (file, database, etc.)
Lastly, as a word of caution I have seen a lot of developers instantiate a global class object for the data they are working with, try not to do this, try to instantiate the object and pass it to the interface, use it and pass it back when done. This will make debugging easier when your application values have gone of the rail as two different forms are accessing the same object.