Question

Is this wrong?

I always get "cb1 and firmware" even if my checkBox2 is checked. I also tried with just & instead of &&.

It was working fine before I had to add it into thread to get UI to update correctly.

 private void MyWorkerThread2()
            {
             if (this.IsChecked(checkBox1) && (this.IsChecked(checkBox2) && (myString == "86.09.0000")))
            {
                MessageBox.Show("cb1 and firmware and cb2");
                Prep.clean();
                startedimage();
                fscreate();
                wipefiles();
            }
            else if ((this.IsChecked(checkBox1) && (myString == "86.09.0000")))
            {
                MessageBox.Show("cb1 and firmware");
                Prep.clean();
                startedimage();
                wipefiles();
            }
            else if (myString == "86.09.0000") 
            {
                MessageBox.Show("firmware");
                if (myThread == null)
                {

                    Prep.clean();
                    startedimage();
                    myThread = new Thread(MyWorkerThread);
                    myThread.IsBackground = true;
                    myThread.Start();
                }
            }

            else
            {

                FactoryReset();
            }
        }



public delegate bool IsCheckedDelegate(CheckBox cb);
        public bool IsChecked(CheckBox cb)
        {
            if (cb.InvokeRequired)
            {
                return (bool)cb.Invoke(new IsCheckedDelegate(IsChecked), new Object[] { cb });
            }
            else
            {
                return cb.Checked;

            }
        }
Was it helpful?

Solution

I always get "cb1 and firmware" even if my checkBox2 is checked.

The fact that checkBox2 is checked isn't going to change the fact that checkBox1 is also checked and so the first if statement succeeds. If checkBox1 wasn't checked, it would fall to the other sets.

It's not clear what you're trying to do here, but I would say the first two if statements need reversed.

OTHER TIPS

It seems like you only want the first to be executed when checkBox2 is NOT checked.

Change:

if ((this.IsChecked(checkBox1) && (myString == "86.09.0000")))

To:

if ((this.IsChecked(checkBox1) && (!this.IsChecked(checkBox2) && (myString == "86.09.0000")))

There are four possibilities here:

none
cb1
cb2
cb1, cb2

Try reordering the code

if (this.IsChecked(checkBox1) && (this.IsChecked(checkBox2) && (myString == "86.09.0000")))    
{
    MessageBox.Show("cb1 and firmware and cb2");
    Prep.clean();
    startedimage();
    fscreate();
    wipefiles();
}
else if ((this.IsChecked(checkBox1) && (myString == "86.09.0000")))
{
    MessageBox.Show("cb1 and firmware");
    Prep.clean();
    startedimage();
    wipefiles();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top