有一个任务,需要把CheckBox当OptionButton来用,并实现互斥,这个问题想了半天,主要是一个逻辑问题,弄通后,在这里记录下。
这里有一个CheckBox数组,名称是Check_Option
Dim LastSlectCheckSn As Integer '用于保证checkbox能当作optionbutton来用
'主程序,窗口加载时先赋值-1
Private Sub Form_Load()
LastSlectCheckSn =-1
End Sub
'Check_Option是一个checkbox数组
Private Sub Check_Option_Click(Index As Integer)
Dim i As Integer
If Index = LastSlectCheckSn Then
Check_Option(Index).Value = 1
Exit Sub
End If
If Check_Option(Index).Value = 0 Then
Check_Option(Index).tag = "0"
Exit Sub
End If
For i = 1 To Check_Option.Count - 1
If i = Index Then
Check_Option(i).tag = "1"
LastSlectCheckSn = Index
Else
Check_Option(i).tag = "0"
End If
Next
For i = 1 To Check_Option.Count - 1
If Check_Option(i).Value <> CInt(Check_Option(i).tag) Then
Check_Option(i).Value = CInt(Check_Option(i).tag)
End If
Next
End Sub