在VB.NET中将整数值从一种形式传递到另一种形式

时间:2015-12-23 03:07:24

标签: vb.net

我正在编写一个有两种形式的程序。一个表单让用户输入多个值,然后进行一些计算。然后它将该信息传递给另一种形式但是我无法弄清楚如何做到这一点。这是我的代码的相关部分。为了解决一些困惑,我试图传递11个值,也是最初,表单2未显示,然后当值从表单1传递到表单2时,表单1消失,表单2是唯一显示的

注意:这不是我的全部代码,我不相信我的所有代码都是必需的(我现在有1000行)但是这是包含我希望传递给其他表单的信息的代码。

很多人显然说这是另一个问题的重复,但是那个问题,他似乎已经知道如何传递变量,但只是有问题(甚至看着他,我无法弄明白)

 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 个答案:

答案 0 :(得分:0)

由于我最近有几乎相同的要求,这是我的解决方案:

  1. 当您的第二张表格关闭时会触发的自定义事件

    Public Event HotKeyFormClosed As EventHandler(Of HotKeyFormClosedEventArgs)
    
  2. 自定义EventArgs类,用于存储要传递给主窗体的值

    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. 在第二个Form处理FormClosed事件并将您的值传递给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. 在主窗体上处理您的自定义事件(此处为HotKeyFormClosed)并提取其值

    AddHandler frmHotKey.HotKeyFormClosed, AddressOf HotKey_FormClosed;
    ...
    Private Sub HotKey_FormClosed(sender As Object, e As HotKeyFormClosedEventArgs)
        'Do stuff with values from e
    End If
    
  5. 我选择了Event方法,因为它将两种形式从另一种形式中分离出来。 人们可以轻松地复制两个表单上的信息,将它们公开并直接通过对象实例访问它。 但我更喜欢事件的可观察方法,因为它提供了灵活性(使用相同事件等的附加形式)。

    P.S。:我在c#中写了我的代码,盲人在这里输入了VB代码,所以要亲切。

答案 1 :(得分:0)

方法期望接收的值/变量(在方法的签名中指定)称为参数。

调用方法时发送给方法的值称为Arguments。

只要调用方法时使用的参数与该方法的参数匹配,就可以传递这些值。

例如(我将尝试将其应用于您的上下文),如果您想创建一个采用特定值的表单实例,您可以在表单的New事件中指定这些参数,如下所示:

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

然后当你调用这个方法时,你会传递参数,如下所示:

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

当我说参数需要匹配参数时,这意味着值的数量,这些值的顺序和值类型必须相同(或者在数字的情况下,参数的类型必须相同或者大于参数的类型)。

只要这是真的,那么你传递这些内容并不重要 - 你可以传递11个单独的参数,你只需要确保你将参数与参数匹配。

希望有所帮助!

相关问题