|
-
Jun 21st, 2006, 04:29 PM
#1
Thread Starter
Fanatic Member
[RESOLVED] Variables between forms
Hi!
How can i declare a variable in a form in a way that I can use its value in another form?
-
Jun 21st, 2006, 04:42 PM
#2
Frenzied Member
Re: Variables between forms
The best course of action really all depends on what you need
You could declare a global variable in a module. This usually should be avoided.
If you declare a variable public in a form and the other form knows about you can access. Here's an example
VB Code:
Public Class Form2
'this is a form
Public MyVariable as String
End Class
Public Class Form1
'this is another form
Private Sub ShowForm1()
Dim f as New Form2
f.ShowDialog()
Messagebox.Show(f.MyVariable)
End Sub
End Class
There are a lot of other ways to do it as well.
You may want to take a look at this article about using multiple forms.
-
Jun 21st, 2006, 04:44 PM
#3
Thread Starter
Fanatic Member
Re: Variables between forms
Hi!!
To do that do i really must enter f.ShowDialog() because that is not very convinient.
Ins't there anything like:
Global MyVariable as String
-
Jun 21st, 2006, 04:56 PM
#4
Frenzied Member
Re: Variables between forms
You don't have to use showdialog. I just used it as an example. Take a look at this example
VB Code:
Public Class Form2
'this is a form
Public MyVariable as String
End Class
Public Class Form1
'this is another form
Dim f as New Form2
Private Sub ShowForm1()
f.Show()
End Sub
Private Sub GetOtherFormValue()
Messagebox.Show(f.MyVariable)
End Sub
End Class
But to answer your question, you can have global variables. But you should only use those if you have to. Here's an example
VB Code:
Module Module1
Publis MyVariable As String
End Module
Public Class Form1
Public Sub SetVariable
MyVariable = "Hello World"
End SUb
End Class
Public Class Form2
Public Sub DisplayVariable
Messagebox.Show(MyVariable)
End Sub
End Class
-
Jun 21st, 2006, 05:01 PM
#5
Hyperactive Member
Re: Variables between forms
Sort of. I would do
VB Code:
Friend MyVariable As String
and then just do your
VB Code:
form#/formname.myvariable.property
http://msdn2.microsoft.com/en-us/lib...y2(d=ide).aspx
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 21st, 2006, 05:20 PM
#6
Thread Starter
Fanatic Member
Re: Variables between forms
Thks guys i'll test them later.
-
Jun 21st, 2006, 05:39 PM
#7
Re: Variables between forms
I strongly suggest that you read the Forms in VB.NET tutorial in my signature. It addresses this and other form-related questions. Also, you should keep in mind that forms are objects like any other, and thus follow the same rules as any other objects. If you want to access a member of an object then you must have a reference to that object.
-
Jun 21st, 2006, 06:18 PM
#8
Re: Variables between forms
Another option would be to expose the variable through a property:
VB Code:
Public Class MyForm
private myVar as string
public ReadOnly Property GetVar() as string
Get
return myVar
end Get
End Property
End Class
Often considered to be the more 'proper' means of exposing a member variable of ANY class.
My usual boring signature: Nothing
 
-
Jun 21st, 2006, 06:58 PM
#9
Thread Starter
Fanatic Member
Re: Variables between forms
Ok i've been looking and reading about forms in VB.Net tutorials from jmcilhinney signature and i conclude that they just don't give me the
information i need (considering that I would prefer not to use a Module) so i decided to explain my problem much better.
I've two forms:
1 - MuShop (the principal)
2 - Customize
In MuShop when u click in Customize button he shows the Customize form with the command Customize.Show().
In Customize I've a checkbox.
What i need it to do?
When i open the customize control and put the value of the checkbox to true he puts the value to a "global" variable then i close Customize form.
I go to MuShop and i click in a Search Button if the "global" variable is true then he searchs in a way if its false he searchs in a different way.
What i've tried?
1)Declared in Customize Class
Public FindWholeWords As Boolean
And when hes leaving the form:
FindWholeWords = SearchWords.Checked (SearchWords is a Checkbox)
Then in Search button (in MuShop form) something like:
if Customize.FindWholeWords = true then
...
elseif Customize.FindWholeWords = False then
...
end if
The problem:
This only work if the customize form stays open, something that can't happen
2) The same as above but instead of Public FindWholeWords As Boolean
i put Friend FindWholeWords As Boolean
The problem:
The same as Above
What did i miss or not tried??
-
Jun 21st, 2006, 11:31 PM
#10
Hyperactive Member
Re: Variables between forms
How about something like
VB Code:
If SearchWords.Checked = True Then
Customize.FindWholeWords = true
Else If
End If
I don't know if thats what you are looking for? (BTW I am doing this off the top of my head so I don't remember about the checked property = boolean).
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 22nd, 2006, 06:51 AM
#11
Thread Starter
Fanatic Member
Re: Variables between forms
And i put that where in MuShop form?
-
Jun 22nd, 2006, 02:29 PM
#12
Hyperactive Member
Re: Variables between forms
Ok I am a little confused with your project...Could you please upload the source code files and the designer files (all except .exe files). I will then give you the correct code.
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 22nd, 2006, 03:26 PM
#13
Thread Starter
Fanatic Member
Re: Variables between forms
Here is it:
PS: i'm noob so do be amazed if the code is a little bit confusing or messy lool
Last edited by Lasering; Jun 22nd, 2006 at 03:57 PM.
-
Jun 22nd, 2006, 03:38 PM
#14
Re: Variables between forms
I would avoid dealing with it using what you are doing, but consider it this way:
You are using the customize form to get information from the user. That information needs to be available both to ANY instance of the customize form (in case the user goes back into it), as well as at least once instance of the MuShop form.
If you keep this information in a single instance of the customize form, then that instance MUST remain in existence as long as that information is needed. However, as you recognize, that's not what you want.
There are two ways to handle this.
1) Put that information in a global variable (declared Public in a module). This will make it available to all form instances, all modules, etc. However, it is generally considered to be undesirable to use global variables, though I suspect most people do to some extent.
2) Create a class that holds the data, and exposes it through a property. Create an instance of that class to receive the data from the Customize form, and retain the data until it is needed by whichever form needs it. That leaves you with the question of where should you create that instance of the class. Since the instance of the class must always be in scope for the code that uses it, one obvious place to put it is declared Public in a module....in other words, make a class to wrap a global variable. Depending on the design of your code, you may find some more restrictive scope for the class instance, but if you could, then you could use that location for suggestion #1, as well.
My usual boring signature: Nothing
 
-
Jun 22nd, 2006, 03:39 PM
#15
Hyperactive Member
Re: Variables between forms
That should do it but I'm not positive since I never received the database. Good Luck! 
VB Code:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
If SearchWords.Checked Then
Teste = True
Else
Teste = False
End If
Timer2.Start()
End Sub
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
-
Jun 22nd, 2006, 03:55 PM
#16
Thread Starter
Fanatic Member
Re: Variables between forms
Thks a lot guys.
PS:I'm taking out the source code.
-
Jun 22nd, 2006, 03:56 PM
#17
Hyperactive Member
Re: Variables between forms
By the way, what is this project anyway?
"Imagination is more important than knowledge" - Albert Einstein, born on March 14th 1879.
Can't find it here on VBForums? Go to the CodeProject. MSDN is your friend . I have such a bad website, my friend decided it would be funny to change the template and he moderates the site for me: visit my site!
"Thinking of you, wherever you are
We pray for our sorrows to end, and hope that our hearts will blend.
Now I will step forward to realize this wish.
And who knows, starting a new journey may not be so hard…
Or maybe it has already begun.
There are many worlds, but they share the same sky
one sky, one destiny..."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|