Find which items have been marked - Help with logic

Asked

Viewed 331 times

3

I have 4 Checkbox, which when selected will assign their values in a variable. The result will be saved in only one field in the BD. When loading these Checkbox, 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 would be 6.

When it comes to taking this data, I need to take this value and select Checkboxs A and B...how to do this account?

  • Could not create an if with possible combinations?

  • Yeah, I think I......

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(value 1).

If you have checkbox 2 and checkbox 4 checked is the equivalent of 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 number b is set.

Implementation:

We’ll use the property Tag of each Checkbox to store the equivalent of its position in a binary representation number:

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;
}

To be able to access Checkbox in a foreach let’s put them in a Dashboard

Method for calculating the value to be stored 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 the 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);
        }
    }
}

Utilizing

To calculate and store the value:

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

To check the Checkbox

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

Note that this code does not need to be changed, whatever the number of Checkbox’s


If you want to do it in a more direct way not using the Panel nor 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;
  • Ramaral, I tried to understand your answer but did not understand... But I was interested in making this account through binaries...can give me an example?

  • I had to leave but I’ll finish the answer.

  • I updated the answer.

  • Congratulations Ramaral! I have not yet tested your answer, but it was all well explained, thank you for your dedication!

  • Any questions just ask.

1

You could do using operators bitwise, using its 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 |, say they are selected 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 operator &:

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 that case with r = 35 then isA, isB and isF would be true.

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

-1

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.