Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My C# project like [WPI Program] to "silent install programs", ex. Google Chrome and Firefox and WinRAR... ect. each one in CheckBox and all in one Panel. now i have a two questions:
1: how do [button install] not enable when Unchecked at least one, or when click it show messagebox to must be select one at least.
2: how do install one by one and show progressbar, when checked all and click on install button (not install all together). really i hope to do this with BackgroundWorker.

What I have tried:

For first question i tried to use Foreach:
C#
private void checkbox(CheckBox check)
    {
    foreach (Control c in panel.Controls)
        {
        if (c is CheckBox)
            {
            if (check.Checked)
                {
                btnInstall.Enabled = true;
                }
            }
        else
            {
            btnInstall.Enabled = false;
            }
        }
    }
Posted
Updated 15-Apr-21 0:01am
v2

1 solution

Firstly, check does not change at all inside the loop - you always look at the parameter value. If that is what you want to do, your method can much simpler:
C#
private void checkbox(CheckBox check)
    {
    btnInstall.Enabled = check.Checked;
    }


If that isn't what you need to do, then look at each checkbox inside the loop.

Every time you go round the loop, you check if the current Control is a CheckBox, and if it isn't you disable the button.
So if there are any non-CheckBox controls after the last Checked CheckBox in the form, you will always disable the button regardless of the Checked state.
Move the Disable instruction outside the loop alltogether:
C#
private void checkbox(CheckBox check)
    {
    btnInstall.Enabled = false;
    foreach (Control c in panel.Controls)
        {
        if (c is CheckBox cb)
            {
            if (cb.Checked)
                {
                btnInstall.Enabled = true;
                break;
                }
            }
        }
    }


And yes, you do want to use a BackgroundWorker to do your install - if you don't the UI will freeze up and you'll never see the progress bar change.
 
Share this answer
 
Comments
User_Michel 15-Apr-21 6:25am    
Thank you for your answer. but in all cases when unchecked one of CheckBoxes, the button become not enable. despite of another CheckBokes still checked!
and about second question, can you give me an example to do that?
OriginalGriff 15-Apr-21 7:00am    
1) Did you try my code?

2) Did you look at the example in the BackgroundWorker documentation?
User_Michel 15-Apr-21 7:17am    
1) Yes. when unchecked one and other checked, the button not enable. but it must be enable as long as other checked.
2) Yes. but not all like this case.
OriginalGriff 15-Apr-21 7:34am    
And?
What did the debugger show you was happening?

My guess is that your checkboxes are inside a separate container, like a GroupBox ...
User_Michel 15-Apr-21 7:58am    
Yes. All CheckBoxes and button in the same Panel.

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