使用 TinyInt 隐藏/显示控件?
我的 GUI 上有 6 个按钮。按钮的可见性可以通过复选框进行配置。选中复选框并保存意味着应该显示相应的按钮。我想知道是否有可能在数据库中拥有一个 TinyInt 列来表示所有 6 个按钮的可见性。
我为按钮创建了一个枚举,它看起来像这样:
public enum MyButtons
{
Button1 = 1,
Button2 = 2,
Button3 = 3,
Button4 = 4,
Button5 = 5,
Button6 = 6
}
现在我想知道如何说,例如使用这一列仅检查button1、button5和button6。有可能吗?
谢谢 :-)
I have 6 buttons on my GUI. The visibility of the buttons can be configured via checkboxes. Checking the checkbox and saving means the correpsonding button should be shown. I am wondering if it is somehow possible to have one TinyInt column in the database which represents the visibility of all 6 buttons.
I created an enum for the buttons, it looks like that:
public enum MyButtons
{
Button1 = 1,
Button2 = 2,
Button3 = 3,
Button4 = 4,
Button5 = 5,
Button6 = 6
}
Now I am wondering how to say that for example only button1, button5 and button6 are checked using this one column. Possible at all?
Thanks :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用标志枚举来代替:
那么按钮的任何组合也是唯一值 - 例如按钮 1 和按钮 2。 Button3 == 5
设置值时,请使用二进制“或”运算符 (|):
要查明是否选择了某个按钮,请使用二进制“与”运算符 (&):
当您想到数字的二进制表示形式:
当你将它们“或”在一起时,你会得到
当你用 MyButtons.Button1 进行“与”时 - 你会回到 MyButtons.Button1:
Use a flags enum instead:
Then any combination of buttons is also a unique value - e.g. Button 1 & Button3 == 5
When setting the value use the binary 'or' operator (|):
To find out if a button is selected use the binary 'and' operator (&):
The reason this works becomes obvious when you think of the binary representations of the numbers:
When you 'or' them together you get
When you 'and' that with MyButtons.Button1 - you get back to MyButtons.Button1:
您必须使用
FlagsAttribute
标记您的枚举:这样您就可以使用:
<<
表示“左移运算符” - 只是设置值的更简单的方法枚举项目。You have to flag your enum with
FlagsAttribute
:so you can use:
<<
means 'left-shift operator' - just a little bit more easy way to set values to enum items.添加 FlagsAttribute,并从字节派生枚举:
Add the FlagsAttribute, and derive the enum from byte: