locked
Using variables from Form1.vb to Form2.vb

    Question

  • I have an equation on Form1.vb that calculates a number.  I need that number to be used in another separate formula that's on Form2.vb.  How do I do this?  I thought using a Module would work but I can't get the function on the Module to "see" the formula and/or variables that I need on Form1.vb.  I am using VB 2005.

    Thanks,

    David

    Thursday, April 29, 2010 11:47 AM

Answers

  • Define a class, Variables, that has properties for each of the variables in your calculation/equation.  Use an instance of that variable on Form1 to hold your variables, and use it to perform your calculation.  Pass an instance of that variable to Form2.  One way to do this could be through a property on Form2 that uses type Variables.

    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Cor LigthertMVP Saturday, May 01, 2010 7:23 AM
    • Marked as answer by dheardsr Saturday, May 01, 2010 11:44 AM
    Thursday, April 29, 2010 12:39 PM
  • If you use a module, or a static class, to hold the variables that you need to share between the forms, then you have several choices in how you arrange those variables and how you update them and access them.

    For instance, if the calculation is really a part of the design of Form1, then you can do the calculation in Form1, but save the result by setting the relevant property of the shared class to the result value. The other forms get the value from the property.   But if the calulation is not paricularly related to Form1, you can set the static class up so that the several inputs that the calculation needs are properties of the class.  Then, in Form1, you establish these properties by getting the user input, validating it, converting it, and setting the property values to the user input.  When that form, or any other, needs to know the result of the calculation, they get the result property of the class. 

    The following example uses a static class to provide a miles per gallon result in Form2 for two values entered in Form1.

    Module Module1
        
    Private myIsValid As Boolean = False
        Private myMiles As Single
        Private myUsage As Single
        Public Property IsValid() As Boolean
            Get
                Return myIsValid
            
    End Get
            Set(ByVal value As Boolean)
                myIsValid = value
            
    End Set
        End Property

        Public Property Miles() As Single
            Get
                Return myMiles
            
    End Get
            Set(ByVal value As Single)
                myMiles = value
                IsValid = 
    False
                If myMiles > 0 And myUsage > 0 Then myIsValid = True
            End Set
        End Property

        Public Property Usage() As Single
            Get
                Return myUsage
            
    End Get
            Set(ByVal value As Single)
                myUsage = value
                IsValid = 
    False
                If myMiles > 0 And myUsage > 0 Then myIsValid = True
            End Set
        End Property

        Public ReadOnly Property Result() As Single
            Get
                If IsValid Then
                    Return myMiles / myUsage
                
    Else
                    Return -1
                
    End If
            End Get
        End Property

    End
     Module

    Public Class Form1
        
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Module1.Miles = Val(TextBox1.Text)
            Module1.Usage = Val(TextBox2.Text)
            
    Dim Frm2 As Form2 = New Form2
            Frm2.Show()
        
    End Sub
    End
     Class

    Public Class Form2
        
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    If Module1.IsValid Then
                Label1.Text = Module1.Result.ToString
            
    Else
                Label1.Text = "No Data!"
            End If
        End Sub
    End
     Class

    • Marked as answer by dheardsr Sunday, May 02, 2010 12:57 PM
    Saturday, May 01, 2010 3:42 AM
  • Hello David,

    I have answered exactly the same question here.

    http://social.msdn.microsoft.com/Forums/de-DE/vbasicexpresseditionde/thread/698ba416-fee0-4f0a-ab86-4462a7450ffa

    Sorry geman. I can translate when you want.

    Use Property.

    best regards

    Ellen

    • Marked as answer by dheardsr Sunday, May 02, 2010 12:56 PM
    Thursday, April 29, 2010 7:16 PM
  • Hi dheardsr,

    if you define a global variable you can open your second form. If you open your form and make some calculations you can close your form with the .close function.

    If you dispose the windows you couldn't get the global variable.

    Regards

    martinwy

    • Marked as answer by dheardsr Sunday, May 02, 2010 12:57 PM
    Thursday, April 29, 2010 7:48 PM
  • I suggest to use static class (module) for hold those variance 

    then use property for call and box it with Namespace.

     

    • Marked as answer by dheardsr Sunday, May 02, 2010 12:57 PM
    Friday, April 30, 2010 6:34 AM
  • Beside the way Rudy shows don't I any of the methods which are showed (I did not look at the one from Ellen).

    A variant on the method from Rudy is to pass simply the form1 to form2 in the constructor or to set it in the property like Rudy describes but then the complete form.

    Be aware you are only passing a reference, not the whole form, so it does not cost any more memory on the stack, which the shared (module) variants showed in this thread do (so are in fact no OOP).

    The one I show you is easier, the one Rudy shows is better.

    In Form2 you put an automatic property 'in versions before 2010 you have to fill in some more

    Friend property Form1

    And then in Form1
    dim frm2 as new Form2
    frm2.Form1 = me

    Now you can use in Form2
    Form1.WhateverThereIsPublicOrFriendOnForm1

     

     


    Success
    Cor
    • Marked as answer by dheardsr Sunday, May 02, 2010 12:58 PM
    Saturday, May 01, 2010 7:22 AM

All replies

  • Define a class, Variables, that has properties for each of the variables in your calculation/equation.  Use an instance of that variable on Form1 to hold your variables, and use it to perform your calculation.  Pass an instance of that variable to Form2.  One way to do this could be through a property on Form2 that uses type Variables.

    Mark the best replies as answers. "Fooling computers since 1971."
    • Proposed as answer by Cor LigthertMVP Saturday, May 01, 2010 7:23 AM
    • Marked as answer by dheardsr Saturday, May 01, 2010 11:44 AM
    Thursday, April 29, 2010 12:39 PM
  • Hello David,

    I have answered exactly the same question here.

    http://social.msdn.microsoft.com/Forums/de-DE/vbasicexpresseditionde/thread/698ba416-fee0-4f0a-ab86-4462a7450ffa

    Sorry geman. I can translate when you want.

    Use Property.

    best regards

    Ellen

    • Marked as answer by dheardsr Sunday, May 02, 2010 12:56 PM
    Thursday, April 29, 2010 7:16 PM
  • Hi dheardsr,

    if you define a global variable you can open your second form. If you open your form and make some calculations you can close your form with the .close function.

    If you dispose the windows you couldn't get the global variable.

    Regards

    martinwy

    • Marked as answer by dheardsr Sunday, May 02, 2010 12:57 PM
    Thursday, April 29, 2010 7:48 PM
  • I suggest to use static class (module) for hold those variance 

    then use property for call and box it with Namespace.

     

    • Marked as answer by dheardsr Sunday, May 02, 2010 12:57 PM
    Friday, April 30, 2010 6:34 AM
  • If you use a module, or a static class, to hold the variables that you need to share between the forms, then you have several choices in how you arrange those variables and how you update them and access them.

    For instance, if the calculation is really a part of the design of Form1, then you can do the calculation in Form1, but save the result by setting the relevant property of the shared class to the result value. The other forms get the value from the property.   But if the calulation is not paricularly related to Form1, you can set the static class up so that the several inputs that the calculation needs are properties of the class.  Then, in Form1, you establish these properties by getting the user input, validating it, converting it, and setting the property values to the user input.  When that form, or any other, needs to know the result of the calculation, they get the result property of the class. 

    The following example uses a static class to provide a miles per gallon result in Form2 for two values entered in Form1.

    Module Module1
        
    Private myIsValid As Boolean = False
        Private myMiles As Single
        Private myUsage As Single
        Public Property IsValid() As Boolean
            Get
                Return myIsValid
            
    End Get
            Set(ByVal value As Boolean)
                myIsValid = value
            
    End Set
        End Property

        Public Property Miles() As Single
            Get
                Return myMiles
            
    End Get
            Set(ByVal value As Single)
                myMiles = value
                IsValid = 
    False
                If myMiles > 0 And myUsage > 0 Then myIsValid = True
            End Set
        End Property

        Public Property Usage() As Single
            Get
                Return myUsage
            
    End Get
            Set(ByVal value As Single)
                myUsage = value
                IsValid = 
    False
                If myMiles > 0 And myUsage > 0 Then myIsValid = True
            End Set
        End Property

        Public ReadOnly Property Result() As Single
            Get
                If IsValid Then
                    Return myMiles / myUsage
                
    Else
                    Return -1
                
    End If
            End Get
        End Property

    End
     Module

    Public Class Form1
        
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Module1.Miles = Val(TextBox1.Text)
            Module1.Usage = Val(TextBox2.Text)
            
    Dim Frm2 As Form2 = New Form2
            Frm2.Show()
        
    End Sub
    End
     Class

    Public Class Form2
        
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            
    If Module1.IsValid Then
                Label1.Text = Module1.Result.ToString
            
    Else
                Label1.Text = "No Data!"
            End If
        End Sub
    End
     Class

    • Marked as answer by dheardsr Sunday, May 02, 2010 12:57 PM
    Saturday, May 01, 2010 3:42 AM
  • Beside the way Rudy shows don't I any of the methods which are showed (I did not look at the one from Ellen).

    A variant on the method from Rudy is to pass simply the form1 to form2 in the constructor or to set it in the property like Rudy describes but then the complete form.

    Be aware you are only passing a reference, not the whole form, so it does not cost any more memory on the stack, which the shared (module) variants showed in this thread do (so are in fact no OOP).

    The one I show you is easier, the one Rudy shows is better.

    In Form2 you put an automatic property 'in versions before 2010 you have to fill in some more

    Friend property Form1

    And then in Form1
    dim frm2 as new Form2
    frm2.Form1 = me

    Now you can use in Form2
    Form1.WhateverThereIsPublicOrFriendOnForm1

     

     


    Success
    Cor
    • Marked as answer by dheardsr Sunday, May 02, 2010 12:58 PM
    Saturday, May 01, 2010 7:22 AM
  • Thanks Acamar for the example.  I have never done this before so an example helps.  I will have to work on this.
    Saturday, May 01, 2010 11:53 AM
  • Thanks all for your input.  I think I know what I have to do I just have never done this before.

    Thanks again for your time and knowledge!

    David

    Saturday, May 01, 2010 11:55 AM