我想做一个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
答案 0 :(得分: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
答案 1 :(得分: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