I have 8 checkboxes in a group and I want to have the decimal value for binair basicly if
checkbox1.checked = true checkbox2.checked = true
then the value has to be (2 ^ 0) + (2 ^ 1) = 3 (the 0 = checkbox1 and the 1 = checkbox2 etc.)
I know the solution could be simple but until now I just can't get it working. The code I have right now is the following:
Private Function AnyOptionsChecked() As Boolean
For Each chk As CheckBox In GroupBox1.Controls
t = t + 1
If chk.Checked = True Then
i = i + 2 ^ t
End If
Next
Return False
End Function
but it seems like t is already at 7 (cause there are 8 checkboxes) before the if/else starts working.
Does someone knows how to solve this problem or is able to point me in the right direction? Thanks.
Your function is returning a boolean (true/false), but really you want to be returning a number if you want to know what options have been checked. How about this:
Private Function OptionsChecked() As Integer
Dim t As Integer = 0
Dim result As Integer = 0
For Each chk As CheckBox In GroupBox1.Controls
If chk.Checked = True Then
result = result + 2 ^ t
End If
t = t + 1
Next
Return result
End Function
So my function returns zero when no options are checked. Otherwise it returns an integer indicating which options have been checked (1 = the first option, 2 = second option, 3 = the first and second option, etc.).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.