Re: [2005] Global Variables
Does your frmMain have a reference to the LoginForm1 class? If so, then you would access strID by using
loginForm1Reference.strID
If not, you will either have to add a reference to a LoginForm1 instance or come up with another method. The alternative to adding a reference to a loginForm1 object really depends on what strID is and how your program works. It doesn't look like strID is a candidate for a static member variable so you'll have to see what makes sense for your app.
Re: [2005] Global Variables
Im not sure if I understand what you're telling me. I am using ApplicationEvents in my prog. The prog runs like this...
1. user will login (I assigned the userID to strID)
2. if login is successful, the FrmMain will display with treeview menu in it.
3. user will choose from a menu
4. another form will appear where I populate a combobox with values (Subjects) depending on the value of strID using SQL SELECT i posted above.
Re: [2005] Global Variables
Okay. Let's just say that you just wanted to get the value of strID.
In your frmMain class definition, which I assume is named FrmMain, put a member variable to a LoginForm1 class along with a way to get and set it: Something like this:
vb.net Code:
Public Class FrmMain
Dim loginForm as LoginForm1
Public Property LoginForm() As LoginForm1
Get
Return loginForm
End Get
Set(ByVal value As LoginForm1)
loginForm = value
End Set
End Property
....
End Class
now before you show frmMain do this
frmMain.LoginForm = Me
Now when you want to access strID anywhere within frmMain do
Me.LoginForm.strID
----
From a design standpoint I don't think what I suggested above is very clean but it will get you access to that variable. You should probably think about what kind of functionality you want and design around that.
Oh and you likely have the value of strID already in the data your displaying as it seems like your using it in a query so you could get the value from your data rather than going back to LoginForm.
Re: [2005] Global Variables
Just a clarification, will the variable be available also with the form the FrmMain is calling? My FrmMain has a treeview menu in it where the user will select from a menu and a form will display on top the the frmMain called FrmDataEntry and this form I have a combobox which will be populated and its values will depend on the value of StrID.
P.S.
I'm sorry I stated in my post #1 that my SQL SELECT is in FrmMain...It's not in FrmMain it should be FrmDataEntry as stated in my post#3 step number 4.
Re: [2005] Global Variables
I got it working already but I guess its not the accepted way, I declared my variable in a Module. I am still interested in learning what is meant by your code.
Quote:
Originally Posted by wy125
Okay. Let's just say that you just wanted to get the value of strID.
In your frmMain class definition, which I assume is named FrmMain, put a member variable to a LoginForm1 class along with a way to get and set it: Something like this:
vb.net Code:
Public Class FrmMain
Dim loginForm as LoginForm1
Public Property LoginForm() As LoginForm1
Get
Return loginForm
End Get
Set(ByVal value As LoginForm1)
loginForm = value
End Set
End Property
....
End Class
now before you show frmMain do this
frmMain.LoginForm = Me
Now when you want to access strID anywhere within frmMain do
Me.LoginForm.strID
----
From a design standpoint I don't think what I suggested above is very clean but it will get you access to that variable. You should probably think about what kind of functionality you want and design around that.
Oh and you likely have the value of strID already in the data your displaying as it seems like your using it in a query so you could get the value from your data rather than going back to LoginForm.
Re: [2005] Global Variables
It's called a Property. You can assign Properties to objects to get and set variables within them, but for your application, it sounds like what you just did; use a Module to store the value globally, is the best, simplest solution.
In wy125's example, he's having the main form create a loginform object and attaching it to a property so that other forms in your program can reference it. Thus, as long as a form knows about frmMain, it can get the info from the loginform.
Re: [2005] Global Variables
Quote:
Originally Posted by Jenner
It's called a Property. You can assign Properties to objects to get and set variables within them, but for your application, it sounds like what you just did; use a Module to store the value globally, is the best, simplest solution.
In wy125's example, he's having the main form create a loginform object and attaching it to a property so that other forms in your program can reference it. Thus, as long as a form knows about frmMain, it can get the info from the loginform.
Yes, I just use a Module. I read somewhere in the forum that its not a good way to do it. Even If I already solved the issue i still would love to know more about what wy125's solution. I really dont understand how the PROPERTY, GET and SET works. Would you kindly explain how the line of codes work?
Re: [2005] Global Variables
Sure. Any class object can have properties. Try this:
Make a new project. Go into the code for Form1 and put the following:
Code:
Public Class Form1
Private strMyString As String
Public Property MyString() As String
Get
Return strMyString
End Get
Set(ByVal value As String)
strMyString = value
End Set
End Property
End Class
Basically, what you've done is made a variable, a String variable (strMyString) and attached it to a Property.
Now, let's add a new form, so make a Form2. Open it's code and put the following:
Code:
Public Class Form2
Private frmMyOwner As Form1
Public Property TheFormThatMadeMe() As Form1
Get
Return frmMyOwner
End Get
Set(ByVal value As Form1)
frmMyOwner = value
End Set
End Property
End Class
We did the same thing here, the only difference is instead of a String variable, we have a variable that can hold a Form1 object.
Ok, now put a button on both forms. On Form1, put this for it's Click event:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
f.TheFormThatMadeMe = Me
f.Show()
End Sub
And put this for the button's Click event in Form2:
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("The form that made me is:" & Me.TheFormThatMadeMe.Name)
End Sub
Run the program. Click the buttons.
Starting to see the picture? I can also manipulate strMyString on Form1 from Form2, because I can do: Me.TheFormThatMadeMe.MyString = "BLAH"
Any questions? :)
Re: [2005] Global Variables
That is a very good example presented by Jenner illustrating how to create and use properties. You could also take a look at this http://msdn.microsoft.com/en-us/library/ms973814.aspx which describes how to create vb.net classes. This will give you a broad overview of classes in general. It's a worth a look, especially if you're coming from a vb6 background.
Re: [2005] Global Variables
Thanks for the example Jenner I will try to play around it and get back heree for further questions.
Thanks for the link also wy125. I come from VB6 and still learning .net
Re: [2005] Global Variables
Hi I get blue squiggly line in form1 and form2 in f.TheFormThatMadeMe and Me.TheFormThatMadeMe respectively
Re: [2005] Global Variables
So what's the error say? That'll give you your answer.
Re: [2005] Global Variables
Take a look at this excellent thread by Shaggy Hiker which discusses various options for passing data across forms: http://www.vbforums.com/showthread.php?t=466253