使用 TinyInt 隐藏/显示控件?

发布于 2024-09-02 10:30:40 字数 361 浏览 4 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耀眼的星火 2024-09-09 10:30:40

使用标志枚举来代替:

[Flags]
public enum MyButtons
{
    None = 0
    Button1 = 1,
    Button2 = 2,
    Button3 = 4,
    Button4 = 8,
    Button5 = 16,
    Button6 = 32
}

那么按钮的任何组合也是唯一值 - 例如按钮 1 和按钮 2。 Button3 == 5

设置值时,请使用二进制“或”运算符 (|):

MyButtons SelectedButtons = MyButtons.Button1 | MyButtons.Button3

要查明是否选择了某个按钮,请使用二进制“与”运算符 (&):

if (SelectedButtons & MyButtons.Button1 == MyButtons.Button1)... 

当您想到数字的二进制表示形式:

MyButtons.Button1 = 000001
MyButtons.Button3 = 000100

当你将它们“或”在一起时,你会得到

SelectedButtons = 000001 | 000100 = 000101

当你用 MyButtons.Button1 进行“与”时 - 你会回到 MyButtons.Button1:

IsButton1Selected = 000101 & 000001 = 000001

Use a flags enum instead:

[Flags]
public enum MyButtons
{
    None = 0
    Button1 = 1,
    Button2 = 2,
    Button3 = 4,
    Button4 = 8,
    Button5 = 16,
    Button6 = 32
}

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 (|):

MyButtons SelectedButtons = MyButtons.Button1 | MyButtons.Button3

To find out if a button is selected use the binary 'and' operator (&):

if (SelectedButtons & MyButtons.Button1 == MyButtons.Button1)... 

The reason this works becomes obvious when you think of the binary representations of the numbers:

MyButtons.Button1 = 000001
MyButtons.Button3 = 000100

When you 'or' them together you get

SelectedButtons = 000001 | 000100 = 000101

When you 'and' that with MyButtons.Button1 - you get back to MyButtons.Button1:

IsButton1Selected = 000101 & 000001 = 000001
伴随着你 2024-09-09 10:30:40

您必须使用 FlagsAttribute 标记您的枚举:

[Flags]
public enum MyButtons : byte
{
    None = 0
    Button1 = 1,
    Button2 = 1 << 1, 
    Button3 = 1 << 2, 
    Button4 = 1 << 3, 
    Button5 = 1 << 4,
    Button6 = 1 << 5
}

这样您就可以使用:

var mode = MyButtons.Button1 | MyButtons.Button5 | MyButtons.Button6;

<< 表示“左移运算符” - 只是设置值的更简单的方法枚举项目。

You have to flag your enum with FlagsAttribute:

[Flags]
public enum MyButtons : byte
{
    None = 0
    Button1 = 1,
    Button2 = 1 << 1, 
    Button3 = 1 << 2, 
    Button4 = 1 << 3, 
    Button5 = 1 << 4,
    Button6 = 1 << 5
}

so you can use:

var mode = MyButtons.Button1 | MyButtons.Button5 | MyButtons.Button6;

<< means 'left-shift operator' - just a little bit more easy way to set values to enum items.

伴我心暖 2024-09-09 10:30:40

添加 FlagsAttribute,并从字节派生枚举:

class Program {
    static void Main(string[] args) {
        MyButtons buttonsVisible = MyButtons.Button1 | MyButtons.Button2;
        buttonsVisible |= MyButtons.Button8;

        byte buttonByte = (byte)buttonsVisible; // store this into database

        buttonsVisible = (MyButtons)buttonByte; // retreive from database
    }
}

[Flags]
public enum MyButtons : byte {
    Button1 = 1,
    Button2 = 1 << 1,
    Button3 = 1 << 2,
    Button4 = 1 << 3,
    Button5 = 1 << 4,
    Button6 = 1 << 5,
    Button7 = 1 << 6,
    Button8 = 1 << 7
} 

Add the FlagsAttribute, and derive the enum from byte:

class Program {
    static void Main(string[] args) {
        MyButtons buttonsVisible = MyButtons.Button1 | MyButtons.Button2;
        buttonsVisible |= MyButtons.Button8;

        byte buttonByte = (byte)buttonsVisible; // store this into database

        buttonsVisible = (MyButtons)buttonByte; // retreive from database
    }
}

[Flags]
public enum MyButtons : byte {
    Button1 = 1,
    Button2 = 1 << 1,
    Button3 = 1 << 2,
    Button4 = 1 << 3,
    Button5 = 1 << 4,
    Button6 = 1 << 5,
    Button7 = 1 << 6,
    Button8 = 1 << 7
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文