-1

我想做一个 For Each 循环,我可以检查每个按钮是启用还是禁用。如果按钮已启用,那么我必须获取每个按钮的标签中的值。我有 26 个按钮,每个按钮都包含不同的值(现金奖励)。*重要提示:此代码需要位于按钮下方,因此每六次按下它就会检查按钮。

伪代码:

btncase1.tag = 5
Begin while statement to go through each button
   Check each button to see if it is enabled
   If button is enabled then obtain values
Next

我拥有的实际代码,但对我来说没有任何意义:

Public Class Form1
Dim button As Button
Dim totalremcases As Integer
Dim btncase As New Control
Dim btncollection As New Microsoft.VisualBasic.Collection()

Private Sub btncase1_Click()
For Each button As Button In btncollection
    If btncase.Enabled Then
        totalremcases = totalremcases + CInt(btncase.Tag)
    End If
Next
4

2 回答 2

5

您可以尝试使用这种方法来解决它

  Public Sub getallcontrolls(controls As System.Web.UI.ControlCollection)
    Dim myAL As New ArrayList()
    For Each ctrl As Control In controls
        If TypeOf ctrl Is Button Then
            If ctrl.Enabled = True Then
                Dim tag As String = ctrl.Tag.ToString()
                myAL.Add(tag)
            End If

        End If
    Next
End Sub
于 2012-06-10T23:43:23.380 回答
0

看起来你正在做一个“交易或不交易”的游戏。

您可以创建一个按钮点击计数器(表单级别变量),以便您可以跟踪已经点击了多少按钮。每次单击按钮时递增计数器。

创建一个函数来累积标签的值。仅当计数器可被 6 整除时才调用此函数。(您说每六次按下按钮时检查一次)

Dim counter As Integer
Dim total As Integer

Private Function AccumulateTags() As Integer
    Dim ctl As Control
    Dim total As Integer
    For Each ctl In Me.Controls
        If TypeOf ctl Is Button Then
            If ctl.Enabled = True Then
                total += Val(ctl.Tag)
            End If
        End If
    Next
    Return total
End Function

Private Function disable(sender As Object)
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is Button AndAlso sender.Equals(ctl) Then
            ctl.Enabled = False
        End If
    Next
End Function

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click, _
              Button2.Click, Button3.Click, Button4.Click, Button5.Click, Button6.Click, Button7.Click
    counter += 1
    If counter Mod 6 = 0 Then 'Checks if counter is divisible by 6
        total = AccumulateTags()
    End If

    MsgBox("Total" & total) 'Displays total. You may also display it in a label if you want
    disable(sender)
End Sub
于 2012-06-11T02:07:59.913 回答