Use TinyInt to hide / show controls?

I have 6 buttons in my GUI. The visibility of buttons can be adjusted using the checkboxes. Checking the checkbox and saving means that the correction button should be displayed. I am wondering if it is possible to have one TinyInt column in a database that displays the visibility of all 6 buttons.

I created an enumeration for buttons, it looks like this:

public enum MyButtons
{
    Button1 = 1,
    Button2 = 2,
    Button3 = 3,
    Button4 = 4,
    Button5 = 5,
    Button6 = 6
}

Now I am wondering how to say that only buttons button1, button5 and button6 are checked using this single column. Maybe at all?

Thank: -)

+5
source share
3 answers

Add FlagsAttribute and print the enumeration from the 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
} 
+1

:

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

- . 1 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
+6

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;

<< " " - .

+3

All Articles