-1

各ボタンが有効か無効かを確認できる For Each ループを実行したいと考えています。ボタンが有効になっている場合は、各ボタンのタグで値を取得する必要があります。それぞれ異なる値 (賞金) を含む 26 個のボタンがあります。*重要: このコードはボタンの下に配置する必要があるため、6 回押すたびにボタンをチェックします。

擬似コード:

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 で割り切れる場合にのみ呼び出します (ボタンが 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 に答える