Find what items have been marked - Help with logic

3

I have 4 Checkboxes, which when selected will assign their values in a variable. The result will be saved in only one field in the DB. When loading these Checkboxes, I need to make an account to know which ones were selected ... How to do this?

Example:

combobox a = 2

combobox b = 4

combobox c = 16

combobox d = 8

If I selected A and B, the sum will give 6.

At the time of getting this data, I need to get this value and select Checkboxs A and B ... how to do this?

    
asked by anonymous 15.04.2015 / 18:45

3 answers

4

The way you are giving the values to each of the check boxes is as if each of them represents a bit.

The weight of each bit is a function of its position in the binary representation:

From right to left the weights are as follows:

Posição 0 - 2^0 = 1
Posição 1 - 2^1 = 2
Posição 2 - 2^2 = 4
Posição 3 - 2^3 = 8
Posição 4 - 2^4 = 16
Posição 5 - 2^5 = 32
Posição 6 - 2^6 = 64
Posição 7 - 2^7 = 128 

Its decimal value is equal to the sum of the weights whose bits are set (with a value of 1).

If you have checkbox 2 and checkbox 4 checked it is the equivalent of the binary representation 00000110 = 6

To know if a bit is set to a decimal value, use the bitwise logic function:

(b & (1 << pos)) != 0;  

This expression returns true if the bit in position pos of the number b is set.

Implementation:

Let's use the Tag property of each CheckBox to save the equivalent of its position in a number in binary representation:

checkBox2.Tag = 1;
checkBox4.Tag = 2;
checkBox8.Tag = 3;
checkBox16.Tag = 4;

Let's create two auxiliary methods.

//Retorna o peso do CheckBox
private int getPeso(CheckBox checkBox)
{
    return (int)Math.Pow(2,checkBox.Tag)
}

// Retorna true se a posição 'pos' em 'valor' está setada
private bool needToCheck(int valor, int pos)
{
    return (valor & (1 << pos)) != 0;
}

In order to be able to access CheckBox in a foreach we will place them in a Dashboard

Method to calculate the value to save in the bank:

private int getValor()
{
    int valor = 0;
    foreach (Control control in panel1.Controls)
    {
        if (control is CheckBox) 
        {
            if(control.Checked) valor = valor + getPeso(control);
        }
    }
    return valor;
}

Method to check CheckBox depending on the value stored in the bank.

private void doChecks(int valor)
{
    foreach (Control control in panel1.Controls)
    {
        if (control is CheckBox) 
        {
            control.Checked = needToCheck(valor, control.Tag);
        }
    }
}

Utilization

To calculate and save the value:

int valor = getValor();
gravarValorNoBanco(valor);

To check CheckBox

int valor = lerValorDoBanco();
doChecks(valor);

Please note that this code does not need to be changed, whatever CheckBox's number

If you want to do it more directly, do not use the CheckBox.Tag

Calculate:

int valor =  0;
if(checkBox2.Checked) valor = valor + 2;
if(checkBox4.Checked) valor = valor + 4;
if(checkBox8.Checked) valor = valor + 8;
if(checkBox16.Checked) valor = valor + 16;
gravarValorNoBanco(valor);

Check:

int valor = lerValorDoBanco();
checkBox2.Checked = (valor & (1 << 1)) != 0;
checkBox4.Checked = (valor & (1 << 2)) != 0;
checkBox8.Checked = (valor & (1 << 3)) != 0;
checkBox16.Checked = (valor & (1 << 4)) != 0;
    
15.04.2015 / 19:21
1

You could do using bitwise operators, using their scalar values as demonstrated with

var a = 1;
var b = 2;
var c = 4;
var d = 8;
var e = 16;
var f = 32;

The test to save the result of the selected fields would be with bitwise | , so let's say a , b and f :

var r = a | b | f; //35

In this case the value to be saved in the database would be the value of r 35 .

To select the checkboxes from the resulting value, you can use the & :

bool isA = (r & a) != 0;
bool isB = (r & b) != 0;
bool isC = (r & c) != 0;
bool isD = (r & d) != 0;
bool isE = (r & e) != 0;
bool isF = (r & f) != 0;

In this case with r = 35 then isA , isB and isF would be true .

So with repetition structures you can solve the problem with just a few lines.

    
15.04.2015 / 20:46
-1

If you have events in checkboxes, you just have to take the value of each and add it to a variable. It depends on the language you are using. If you want to make it simpler that (maybe) it works for everything, you can simply add the selected values to a vector and in the end add all the positions.

But the solution is to check WHEN the buttons were pressed and add the value. Not after.

    
15.04.2015 / 18:59