问题描述
亲爱的,
我的要求是从数据库中获取字符串值(deliveryChannel)并根据该值获取我的4个复选框正在工作....
Ex:deliveryChannel值如0000,0001,0100......
第一位置复选框1
第二位复选框2
第三位复选框3
第四位复选框4
谢谢和问候,
Ibrahim shaik
我尝试了什么:
我的代码是:
checkbox1.visible = Convert.ToBoolean(deliveryChannel [0] .equal('1'));
checkbox2.visible = Convert.ToBoolean(deliveryChannel [1] .equal('1'));
checkbox3.visible = Convert.ToBoolean(deliveryChannel [2] .equal('1'));
checkbox4.visible = Convert.ToBoolean(deliveryChannel [3] .equal('1'));
以上代码工作正常,但我想使用en实现上面的代码嗯和bitwise opertors ....
Dear all,
My requirement is getting string value(deliveryChannel) from database and based on that value my 4 check boxes are working....
Ex: deliveryChannel value like "0000","0001","0100"....
1st position checkbox1
2nd position checkbox2
3rd position checkbox3
4th position checkbox4
Thanks and Regards,
Ibrahim shaik
What I have tried:
My code is:
checkbox1.visible=Convert.ToBoolean(deliveryChannel[0].equal('1'));
checkbox2.visible=Convert.ToBoolean(deliveryChannel[1].equal('1'));
checkbox3.visible=Convert.ToBoolean(deliveryChannel[2].equal('1'));
checkbox4.visible=Convert.ToBoolean(deliveryChannel[3].equal('1'));
Above code is working fine but I want implement above code using enum and bitwise opertors....
推荐答案
string DeliveryChannel = "0101";
int BinaryDeliveryChannel = Convert.ToInt16(DeliveryChannel, 2);
checkbox1.visible = (1 & BinaryDeliveryChannel) > 0;
checkbox2.visible = (1 & BinaryDeliveryChannel >> 1) > 0;
checkbox3.visible = (1 & BinaryDeliveryChannel >> 2) > 0;
checkbox4.visible = (1 & BinaryDeliveryChannel >> 3) > 0;
这里发生的是你正在将你想要的位移到右边并通过按位与AND检查它是否为ON
你的第一位不需要转移。该操作执行如下:
What happens here is you are moving your desired bit to the right-hand side and checking if it is ON by bitwise AND-ing with 1.
Your first bit doesn't need a shifting. The operation performs as:
0001 - for 1<br />
0101 - for the channel mask<br />
-----<br />
0001 - Result of the bitwise AND
因此,结果为1,并指定第一个复选框ON。
要检查第二位,您需要将其向右移动一位。因此0101变为0010.也就是说,传送通道掩模的所需位(右起第二个)移动到最右边的位置。该操作执行如下:
Thus, the result is one, and would designate the first checkbox ON.
To check the second bit you need to move it one bit right. So 0101 becomes 0010. That is, the desired bit of the delivery channel mask (second from right), moves to the rightmost position. The operation performs as:
0001 - for 1<br />
0010 - for the channel mask right-shifted one bit<br />
-----<br />
0000 - Result of the bitwise AND
因此,按位AND为零,因此将指定第二个复选框不可见。
等等......希望这会有所帮助。
Thus, the bitwise AND is zero, and hence would designate that, the second checkbox will not be visible.
And so on... Hope this helps.
这篇关于使用c#.net进行Winform .................................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!