Question Sharing variable in a module that can come from two different forms

erospace

Member
Joined
Dec 15, 2012
Messages
5
Programming Experience
Beginner
Hi. I've spent a lot of time searching on google on how to do what I describe below but I have not had any luck. I am using Visual Studio Express 2012, and I am very new to vb.net and only have programming experience from a couple of college classes. I have successfully been getting variables from my forms, but I don't know how to do what I describe below. I feel like there is a good chance that I am going about it the wrong way, and I need to do it some other way. Thank you ahead of time for any input.

Lets say I have two forms, Form1 and Form2
Each form has a text box called TextBox1

When the enter button is hit Form1 the following happens:
A public variable FormSelected gets a value of "Form1"
Sub Main1 is called

When the enter button is hit on Form2 the following happens:
A public variable FormSelected gets a value of "Form2"
Sub Main1 is called

Once Sub main an if statement is ran to see what form was used

If FormSelected = "Form1" Then

Else

End If

In the If statement I want to be able to make a variable the same as the form selected so my entire program can run off that varialbe when getting the value from TextBox1

Here is how I can get the variables from Form1

Dim Variable1 as String
Variable1 = Form1.TextBox1.Text

Here is how I can get the variables from Form2
Dim Variable1 as String
Variable1 = Form2.TextBox1.Text

I want to be able to do something like

Dim FormName as Object (I don't know what it would be set too?)

If FormSelected = "Form1" Then
FormName = Form1
Else
FormName = Form2
End If

To get the variable from either form now I want something like this

Dim Variable1 as String
Variable1 = FormName.TextBox1.Text

Thanks!
 
There are various ways this could be achieved but here's probably the easiest option for you. Get rid of your FormName variable and your Main1 method. Instead, declare a variable of type TextBox. On each of your forms, when the user clicks the Button, have the form assign its own TextBox1 to that variable. The rest of the application then simply accesses the appropriate TextBox from that variable. A TextBox is a TextBox so the application code doesn't have to care what form it came from.

They may go over your head but, if you're interested in some other options, let me know and I'll post a couple.
 
jmcilhinney thank you very much for the reply and help. I played around with what you said, and I think I did what you were saying. I just wanted to double check and make sure that I have to set the Var1 As a TextBox Publicly like I have in Module1. On my program, I have a lot of textboxes, combo boxes, and check boxes, so I guess I'll be making a lot of public variables, and assigning them all in the enter button that calls the Sub main if I did this the way you were mentioning. Here is what I have:

FORM 1 CODE:
Public Class Form1
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Var1 = Me.TextBox1
Call main()
End Sub
End Class

FORM 2 CODE:
Public Class Form2
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Var1 = Me.TextBox1
Call main()
End Sub
End Class

MODULE 1 CODE:
Module Module1
Public Var1 As TextBox
Public Sub main()
Dim variable1 As String
variable1 = Var1.Text
Debug.Print(" This is the variable1 " & variable1)
End Sub
End Module

Thanks again for all your help!
 
That's pretty much it but you should absolutely not be naming that method 'main'. That is the name of the entry method for your app and should not be used for anything else.

Is the data in the controls going to change between when you set this global variable and when you use the data?
 
So your saying instead of Public Sub main() in my Module1 I should rename it to something like Public Sub MainProgram() or something like that?

Some of the data in the controls change the Sub main() in Module1. For instance I have something like this for a couple of variables where the variable gets divided by a number to update the variable:

Dim variable1 As String
variable1 = Var1.Text
variable1 = variable1 / 20

End of program

Thanks!
 
I'm definitely not suggesting that you change the name to MainProgram. As with all identifiers, the of that method should be something that describes its purpose, which MainProgram certainly does not.

As for the rest, you haven't answered my question. Please read the words and answer the question asked. Will the data in the control, i.e. the Text of the TextBox, change between when you set the global variable and when you use it? If not then what's the point of remembering the form or the control when all you actually need to remember is the data in the control?
 
Thanks again. Sorry, I didn't understand the question. I'm still really new to a lot of this. If I am understanding this correctly, then the data in the control will not change. The user fills in all the text boxes on the form, hits enter, and the numbers in the text boxes are used as variables for an API in SolidWorks. So all the textboxes are like lengths and dimensions of parts I am making. I just have two different programs that use some of the same code to run in SolidWorks. For instance the form for both programs has a textbox for the input of length. Thank you for all of your help. I apologize that my questions and responses don't make much sense since I'm new to a lot of this.
 
If the data won't change then, as I said, you don't need to care what form or what TextBox it is. Make the global variable type String and then, when the user clicks the Button on the form, just assign the Text of the appropriate TextBox to that variable. The code that then uses the variable doesn't have to concern itself with where the text came from.
 
Back
Top