Results 1 to 17 of 17

Thread: [RESOLVED] Variables between forms

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [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?
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    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:
    1. Public Class Form2
    2.    'this is a form
    3.     Public MyVariable as String
    4. End Class
    5.  
    6. Public Class Form1
    7.    'this is another form
    8.  
    9.    Private Sub ShowForm1()
    10.         Dim f as New Form2
    11.         f.ShowDialog()
    12.         Messagebox.Show(f.MyVariable)
    13.    End Sub
    14. 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.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    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:
    1. Public Class Form2
    2.    'this is a form
    3.     Public MyVariable as String
    4. End Class
    5.  
    6. Public Class Form1
    7.    'this is another form
    8.  
    9.   Dim f as New Form2
    10.  
    11.    Private Sub ShowForm1()
    12.         f.Show()
    13.    End Sub
    14.  
    15.    Private Sub GetOtherFormValue()
    16.        Messagebox.Show(f.MyVariable)
    17.    End Sub
    18. 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:
    1. Module Module1
    2.    Publis MyVariable As String
    3. End Module
    4.  
    5. Public Class Form1
    6.  
    7.    Public Sub SetVariable
    8.        MyVariable = "Hello World"
    9.    End SUb
    10. End Class
    11.  
    12. Public Class Form2
    13.      Public Sub DisplayVariable
    14.           Messagebox.Show(MyVariable)
    15.      End Sub
    16. End Class
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  5. #5
    Hyperactive Member sheikh78's Avatar
    Join Date
    Apr 2006
    Location
    C:/
    Posts
    423

    Re: Variables between forms

    Sort of. I would do
    VB Code:
    1. Friend MyVariable As String
    and then just do your
    VB Code:
    1. 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..."

  6. #6

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Variables between forms

    Thks guys i'll test them later.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Variables between forms

    Another option would be to expose the variable through a property:

    VB Code:
    1. Public Class MyForm
    2.   private myVar as string
    3.  
    4.  public ReadOnly Property GetVar() as string
    5.    Get
    6.      return myVar
    7.    end Get
    8.  End Property
    9.  
    10. End Class

    Often considered to be the more 'proper' means of exposing a member variable of ANY class.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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??
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  10. #10
    Hyperactive Member sheikh78's Avatar
    Join Date
    Apr 2006
    Location
    C:/
    Posts
    423

    Re: Variables between forms

    How about something like

    VB Code:
    1. If SearchWords.Checked = True Then
    2. Customize.FindWholeWords = true
    3. Else If
    4. 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..."

  11. #11

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Variables between forms

    And i put that where in MuShop form?
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  12. #12
    Hyperactive Member sheikh78's Avatar
    Join Date
    Apr 2006
    Location
    C:/
    Posts
    423

    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..."

  13. #13

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    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.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  14. #14
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    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

  15. #15
    Hyperactive Member sheikh78's Avatar
    Join Date
    Apr 2006
    Location
    C:/
    Posts
    423

    Re: Variables between forms

    That should do it but I'm not positive since I never received the database. Good Luck!
    VB Code:
    1. Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    2.         If SearchWords.Checked Then
    3.             Teste = True
    4.         Else
    5.             Teste = False
    6.         End If
    7.         Timer2.Start()
    8.     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..."

  16. #16

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Variables between forms

    Thks a lot guys.

    PS:I'm taking out the source code.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  17. #17
    Hyperactive Member sheikh78's Avatar
    Join Date
    Apr 2006
    Location
    C:/
    Posts
    423

    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
  •  



Click Here to Expand Forum to Full Width