在此代码中创建每个变量Dynamic Visual Basic 2010

时间:2013-03-21 00:16:17

标签: arrays vb.net checkbox

很抱歉这段代码很长。在我正在设计的游戏中,我有这个代码从checkbox.checked if语句开始,当前每个复选框被重复5次 - 每个复选框等于游戏中玩家手中的牌。

我想知道的是我能做些什么才能让它变得充满活力。即CardCheckBox1.Checked对象/方法。如上所述,代码重复5次,因为我有5个现有的复选框。有没有办法将复选框放入数组或集合中,所以当我单击一个复选框并点击播放按钮时,它将相当于击中CardCheckBox2.checked = true,CardCheckBox2.checked = true等等。一旦我想出如何为了使该部分动态化,我终于可以开始使其余代码动态化,因为我假设制作动态标签代码的程序与复选框等非常相似。

我已经

Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5}

在模块级别,Form_Load过程,甚至是Play_Button过程中发布,但我总是在CardCheckBox(n)上找到一个空引用异常。当我这样做时,检查部分代码

If CardCheckBoxArray(0).Checked = True And Player1HandGroup(Number1).QuantityInteger > 0 Then

所以我不知道该去哪里。代码是我的整个PlayButton_Click过程,除了其他CardCheckBox if语句。

Private Sub PlayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlayButton.Click
        Dim n As Integer
        Dim WeaponDiscardInteger As Integer = 1
        AtkPlayerDialog.ShowDialog()
        'Code for choosing which player to attack
        If AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.Cancel Then
            n = 2
        ElseIf AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.OK Then
            n = 1
        ElseIf AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.Abort Then
            n = 3
        ElseIf AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.Retry Then
            n = 4
        ElseIf AtkPlayerDialog.DialogResult = Windows.Forms.DialogResult.Ignore Then
            n = 5
        End If

'playing card 1
        If CardCheckBox1.Checked = True And Player1HandGroup(Number1).QuantityInteger > 0 Then
            'Subtract Hitpoints when damage is delt
            Player1HandGroup(n).HitPoints -= Player1HandGroup(Number1).DamageInteger
            HitPoints1.Text = Player1HandGroup(1).HitPoints.ToString
            HitPoints2.Text = Player1HandGroup(2).HitPoints.ToString
            HitPoints3.Text = Player1HandGroup(3).HitPoints.ToString
            HitPoints4.Text = Player1HandGroup(4).HitPoints.ToString
            HitPoints5.Text = Player1HandGroup(5).HitPoints.ToString
            'When player plays hand, card quantity is removed from hand to discard pile.
            Player1HandGroup(Number1).QuantityInteger -= 1
            DiscardGroup(Number1).QuantityInteger += 1
            'Shuffle Deck from Discard Pile if Deck is out of cards
            Call DiscardPile()
            'Reset Number Generator, unless weapon isn't discard
            Number = (DeckGroup(Rnd.Next(0, DeckGroup.Count)).ID)
            If DeckGroup(Number).QuantityInteger > 0 Then
                'Grab New Card From Deck
                DeckGroup(Number).QuantityInteger -= 1
                Player1HandGroup(Number).QuantityInteger += 1
                Card1Type = Player1HandGroup(Number).CardType
                CardCheckBox1.Text = Player1HandGroup(Number).CardNameString
                Number1 = Number
            Else
                Call PlayElse()
            End If

1 个答案:

答案 0 :(得分:0)

我在大多数表单代码中使用复选框数组,然后将表单加载事件移出表单级别范围。当它在forn_load事件子例程中时,它与模块级范围不同。

所以你会......

Dim CardCheckBoxArray() As CheckBox = {CardCheckBox1, CardCheckBox2, CardCheckBox3, CardCheckBox4, CardCheckBox5}


Sub Form_Load(blah blah blah)
  ...
End Sub

Sub Play_Button(blah blah blah)
  ...
End Sub

等等