[英]VB.net Decimal value of Binair checkbox
I have 8 checkboxes in a group and I want to have the decimal value for binair basicly if 我在一个组中有8个复选框,并且基本上,如果要设置binair的十进制值,
checkbox1.checked = true checkbox2.checked = true 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.) 那么该值必须为(2 ^ 0)+(2 ^ 1)= 3(0 =复选框1和1 =复选框2等)
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. 但是在if / else开始工作之前,看来t已经是7(因为有8个复选框)。
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.).
否则,它返回一个整数,指示已检查的选项(1 =第一个选项,2 =第二个选项,3 =第一个和第二个选项,依此类推)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.