Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I have 8 checkboxes and I want to set the value of these checkboxes in One Byte value. That is each checkbox values(either 0 or 1) occupy one bit. How to write logic for this in C# ?

Example Checkbox1 is set to 1 and remaining checkboxes set to 0 then the value of 1 byte is 0x01(in binary 0000 0001). Like wise i need combinations for all the checkboxes in code.

Please anyone help me.
Posted
Comments
Richard MacCutchan 9-Jan-14 7:26am    
Are you saying that you cannot figure out the values: 0x01, 0x02, 0x04, 0x08, 0x10 etc?

try like this..


C#
byte b = 0x01;
BitArray flags = new BitArray(new byte[] { b });
CheckBox1.Checked = flags[0];
CheckBox2.Checked = flags[1];
CheckBox3.Checked = flags[2];
CheckBox4.Checked = flags[3];
CheckBox5.Checked = flags[4];
CheckBox6.Checked = flags[5];
CheckBox7.Checked = flags[6];
CheckBox8.Checked = flags[7];


Convert 8 bit data (BitArray) to byte:
C#
byte[] bytes = new byte[1];
 flags.CopyTo(bytes, 0);
 byte result = bytes[0];
 
Share this answer
 
v2
Comments
BillWoodruff 9-Jan-14 9:10am    
+5 nice work !
Karthik_Mahalingam 9-Jan-14 9:12am    
Thanks Bill :)
Richard MacCutchan 9-Jan-14 10:51am    
Fine except he wants the values in a single byte.
Karthik_Mahalingam 9-Jan-14 11:24am    
added Richard
Richard MacCutchan 9-Jan-14 12:07pm    
Why not just use a single mask byte in the first place?
To prevent you from asking basic questions like this, you would be best served by learning to do math in binary and hexadecimal.

I'm dead serious on this. Google "Binary math" and "bitwise operators" and start reading. You can't go through life writing code without knowing this stuff.
 
Share this answer
 
I'm going to assume that in addition to setting the 'Checked property of the CheckBoxes based on a byte value ... as Karthik neatly shows you how to do, that you also want to read the CheckBoxes' checked state, and construct a byte value.
C#
// declare a list of CheckBoxes
private List<checkbox> cbList = new List<checkbox>(); 

// somewhere in code, perhaps in a Form Load Event initialize the List
cbList.AddRange(new CheckBox[]
{
  checkBox1, checkBox2, checkBox3, checkBox4, checkBox5, checkBox6, checkBox7, checkBox8
});

// function to read the CheckBoxes and return a byte value
private byte getByteFromCheckBoxes()
{
    byte b = 0x0;

    for (int i = 0; i < 8; i++)
    {
        if(cbList[i].Checked) b += Convert.ToByte(Math.Pow(2, i));
    }

    return b;
}

// test the function at run-time
private void button1_Click(object sender, EventArgs e)
{
    byte checkByte = getByteFromCheckBoxes();
}
For eliminating the computation in the function above ... which I'd want to do if generating a byte from the CheckBoxes was occurring frequently ... I'd consider using a Dictionary:
private Dictionary<CheckBox, byte> dctChkByte;

// initialize somewhere in code
dctChkByte = new Dictionary<checkbox,>
{
    {checkBox1, 0x01},
    {checkBox2, 0x02},
    {checkBox3, 0x04},
    {checkBox4, 0x08},
    {checkBox5, 0x10},
    {checkBox6, 0x20},
    {checkBox7, 0x40},
    {checkBox8, 0x80},
};

// use in a function
private byte getByteFromCheckBoxes2()
{
    byte b2 = 0x0;

    foreach (CheckBox theCheckBox in dctChkByte.Keys)
    {
        if (theCheckBox.Checked) b2 += dctChkByte[theCheckBox];

        // note that you cannot do this: write Microsoft
        // and complain if this makes you unhappy !
        // b2 += (theCheckBox.Checked) ? dctChkByte[theCheckBox] : 0x0;
    }

    return b2;
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900