データグリッド ビューにチェックボックス列があり、ユーザーが 7 つのボックスのみをチェックするように制限したいと考えています。また、チェックボックスをクリックするたびに何かをしたいです。このように (疑似コードのみ):
While counter is not equal to 7 {
Check if the user clicked a checkbox (say, some random checkbox 1) {
(when I say random, I mean not in order, or in no particular order)
copy row data from where the user clicked the checkbox onto another form (data 1)
increment counter to 1
display msgbox saying that the user clicked '1 out of 7'}
Check if the user clicked another random checkbox (2) {
copy row data from where the user clicked the checkbox onto another form (data 2)
increment counter to 2
display msgbox saying that the user clicked '2 out of 7'}
.
.
.
Check if the user clicked another random checkbox (7) {
copy row data from where the user clicked the checkbox onto another form (data 7)
increment counter to 7
display msgbox saying that the user clicked '7 out of 7'}
If counter is more than 7, exit sub and display msgbox('You can only select 7 names')
これを機能させるために、いくつかのレイヤーと FOR_NEXT ループのさまざまな配置を試しましたが、機能させることはできません! これが私のコードです:
Private Sub dgvPaidComms_CellContentClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPaidComms.CellContentClick
Dim counter As Integer = 0
For Each row As DataGridViewRow In dgvPaidComms.Rows
Dim selected As Boolean = DataGridView1.Rows(e.RowIndex).Cells(0).Value
If selected = True Then
counter += 1
End If
Next
If counter > 7 Then
MsgBox("You can only select 7 names.")
Exit Sub
End If
End Sub
しかし、DGV (50 行以上) のすべての行をループ処理するため、MsgBox("You can only select 7 names.")
. また、通常の FOR-NEXT ループ (つまり、通常のカウンターのようFor counter As Integer = 0 to 7 ... Next
なもの) を使用して、そのFor Each row As DataGridViewRow In dgvPaidComms.Rows...
内部に配置しようとしましたが、その逆も同様で、がっかりしました。
迷っています。私は本当に混乱しています。私はそれがDataGridView1.Rows(e.RowIndex).Cells(0).Value
1つのCellClickイベント(つまり、1つのチェックボックスがオンになっていることを意味します。コードを実行しようとすると何が起こるかというと、1つのチェックボックスをオンにすると、MsgBox("You can only select 7 names.")
ポップアウトするためです。実際にはすべての行を実行し、通常の FOR_NEXT (上記で説明) を使用すると、無限ループになります)。
誰かがこの混乱を解消するのを手伝ってくれますか?