0

I'm writing a program that has two forms. One form gets the user to enter multiple values, and then does some calculations. Then it passes that information to another form However I can't figure out how to do it. Here is a relevant part of my code. To head some confusion, I am trying to pass 11 values, also initially, form 2 is not shown, and then when the values are passed from form 1 to form 2, then form 1 goes away and form 2 is the only one that shown

NOTE: This is not all my code, I don't believe all my code is required (I have 1000 lines right now) However this is the code with the information I want to be passed to the other form.

A lot of people are apparently saying that this is a duplicate of another question, however that question, he seems to already know how to pass the variables, but is just having issues with it (and even with looking at his, i cant figure it out)

 Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    'declarations
    Dim intNormal As Integer
    Dim intChildren As Integer
    Dim intBonanza As Integer
    Dim intDiamond As Integer
    Dim intPictureFrame As Integer
    Dim intKite As Integer
    Dim intCrazyT As Integer
    Dim intLetterX As Integer
    Dim int2PostageStamp As Integer
    Dim intPick7 As Integer
    Dim intJackpot As Integer

    Validate()
    If txtNormal1.Enabled = False Then
        intNormal = intNormInput
    Else
        intNormal = CalcNormalBooks()
    End If
    If txtChildren1.Enabled = False Then
        intChildren = intChildInput
    Else
        intChildren = calcChildrensBooks()
    End If
    If txtBonanza1.Enabled = False Then
        intBonanza = intBonInput
    Else
        intBonanza = calcBonanza()
    End If
    If txtSpecial1.Enabled = False Then
        intSpecial = intSpeInput
    Else
        intSpecial = calcSpecialBooks(intSpecial)
    End If
    If txtDiamond1.Enabled = False Then
        intDiamond = intDiaInput
    Else
        intDiamond = calcDiamond(intSpecial)
    End If
    If txtPictureFrame1.Enabled = False Then
        intPictureFrame = intPicInput
    Else
        intPictureFrame = calcPictureFrame(intSpecial)
    End If
    If txtKite1.Enabled = False Then
        intKite = intKiteInput
    Else
        intKite = calcKite(intSpecial)
    End If
    If txtCrazyT1.Enabled = False Then
        intCrazyT = intCrazyInput
    Else
        intCrazyT = calcCrazyT(intSpecial)
    End If
    If txtLetterX1.Enabled = False Then
        intLetterX = intLettInput
    Else
        intLetterX = calcLetterX(intSpecial)
    End If
    If txt2PostageStamp1.Enabled = False Then
        int2PostageStamp = intPostInput
    Else
        int2PostageStamp = CalcPostageStamp(intSpecial)
    End If
    If txtPick71.Enabled = False Then
        intPick7 = intPickInput
    Else
        intPick7 = calcPick7(intSpecial)
    End If
    If txtJackpot1.Enabled = False Then
        intJackpot = intJackInput
    Else
        intJackpot = calcJackpot()
    End If
End Sub
2
  • Possible duplicate of Variable Value Passing to another Form, VB.Net Commented Dec 23, 2015 at 5:06
  • How many values you want to pass from one form to another ?, Does both forms are opened at same time or what ?? Commented Dec 23, 2015 at 5:06

2 Answers 2

0

Since I had almost the same requiremnt lately here is my solution:

  1. Custom Event which fires when your 2nd Form is closing

    Public Event HotKeyFormClosed As EventHandler(Of HotKeyFormClosedEventArgs)
    
  2. Custom EventArgs class where you store your values you want to pass to Main Form

    Public Class HotKeyFormClosedEventArgs
    Inherits EventArgs
    
    'Your properties here
    
    Public Sub New(...) 'your params here
        MyBase.New()
        'set your properties here
    End Sub
    End Class
    
  3. On 2nd Form handle FormClosed event and pass your values to EventArgs

    Private Sub HotKey_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs)
        RaiseEvent HotKeyFormClosed(Me, New HotKeyFormClosedEventArgs(...))  'your params here
    End Sub
    
  4. On Main Form handle your custom event (here HotKeyFormClosed) and extract its values

    AddHandler frmHotKey.HotKeyFormClosed, AddressOf HotKey_FormClosed;
    ...
    Private Sub HotKey_FormClosed(sender As Object, e As HotKeyFormClosedEventArgs)
        'Do stuff with values from e
    End If
    

I have chosen the Event approach since it decouples the two forms from another. One could easily duplicate the information on both forms, make them public and access it directly thru an object instance. But I like the observable approach from the events more due to it gives mor flexibility (additonal forms using the same events etc.)

P.S.: I wrote my code in c# and blind entered the VB code here so be gracious.

Sign up to request clarification or add additional context in comments.

2 Comments

so where you put the comments "Your Params Here" do you mean the variables I need to pass? We weren't taught the term Parameters in class yet
Yes, parameters are varaibles passed to a method. SInce you didn´t write in your question which variables you want to pass I wrote a generic answer.
0

The values/variables that a method expects to receive (specified in the method's signature) are called Parameters.

The values sent to a method when the method is called are called Arguments.

As long as the arguments used when calling a method match the parameters for that method, those values can be passed.

For example (and I'll try to apply this to your context), if you want to create an instance of a form that takes certain values, you can specify those parameters in the form's New event, like so:

Public Sub New(someInt As Integer)
     'do something with someInt here
End Sub

Then when you call this method you'd pass it the arguments, like so:

Dim myInt As Integer = 10
Dim newForm As myForm = New myForm(myInt)

When I say the arguments need to match the parameters, that means the number of values, the order of those values, and the value types must be the same (or in the case of numbers the parameter's type must be the same or larger than the argument's type).

As long as that is true, then it shouldn't really matter how you pass these - you could pass 11 individual arguments, you just have to make sure you are matching the argument to the parameter.

Hope that helps!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.