Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to pass values between forms in vb.net? (Without using a public variable)

Tags:

vb.net

I have an application that uses a second form with textBox and button, and i need to pass the text from textBox to the first form. I know how to do it with a public variable, but i would like to know if there is another way to do it without using a public variable.

like image 661
Liewyec Avatar asked Sep 23 '13 18:09

Liewyec


People also ask

How do you pass a variable from one form to another in VB net?

You can use module class in vb.net, File -> Add New Item -> Module class, and declare all global variables here and you can use these variable any where in the current project.

How pass textbox value from one form to another in VB net?

In order to retrieve a control's value (e.g. TextBox. Text ) from another form, the best way is to create a module and create a property for the private variable. Then in the textbox's TextChanged event use the property getCustomerFirstNameSTR to hold the textbox's text.


2 Answers

Yes!

If the forms are part of the same solution, you simply write:

Dim MyVal1 As Integer = Form2.MyVal

MyVal1 exists in whatever form you write it in, and MyVal2 comes from Form2. These variables can only be shared if the forms have the right privacy settings.

If the two forms are part of separate applications, the only way i know of passing variables around is through constructors.

EDIT:

In your case with a textbox, you may be able to apply similar a solution:

Dim val As String = Form2.TextBox1.Text
like image 157
char1es Avatar answered Oct 06 '22 22:10

char1es


You can use PUBLIC properties on the second form to read/write info to the other form.

You can overload the NEW method to pass variable during declaration.

You can create a public sub/function on the second form and pass variable byval or byref.

But, I would NEVER use a public variable. That is bad programming.

like image 29
Steve Avatar answered Oct 06 '22 20:10

Steve