Hey all

I've got a form and several checkboxes. Each checkbox (if checked) runs a batch file which in turn installs a piece of software.

The 2 is question are Acrobat Pro and Acrobat Standard - obviously only one can be installed. Therefore when one is checked by a user, another should become disabled and vice versa. It sounds simple but I can't seem to get it to work.

First off I tried adding if loops to the CheckedChanged event for each checkbox, but that only worked one way (e.g. checking a box would disable the other, but unchecking it wouldn't re-enable the other). So now I've tried sticking the if loop in the main namespace.

public Form1()
        {
            InitializeComponent();
            if (acrobat_pro_chkbox.Checked)
            {
                acrobat_standard_chkbox.Enabled = false;
            }
            if (acrobat_standard_chkbox.Checked)
            {
                acrobat_pro_chkbox.Enabled = false;
            }
        }

I can't see why this wouldn't work? Checking either box doesn't seem to do anything at all.

Any help would be appreciated.

You have placed your code in the form constructor.
It should go in a Checked event handler for the check boxes.

Thanks for the reply.

I had it in the CheckedChanged event originally, but as I said I can't reverse the process (e.g. Adobe Standard is checked, so Adobe Pro is disabled. Adobe Standard is unchecked, Adobe Pro should become enabled but doesn't - it stays disabled.

private void acrobat_standard_chkbox_CheckedChanged(object sender, EventArgs e)
        {
            if (acrobat_standard_chkbox.Checked)
            {
                acrobat_pro_chkbox.Enabled = false;
            }
        }

        private void acrobat_pro_chkbox_CheckedChanged(object sender, EventArgs e)
        {
            if (acrobat_pro_chkbox.Checked)
            {
                acrobat_standard_chkbox.Enabled = false;
            }
        }

Just force the Checked property to false instead of the Enabled property. That way they both stay enabled, but only one is ever checked.
Alternatively, you could use RadioButtons instead. These automatically uncheck when one is clicked.

For this kind of behavior it is best to use two radiobuttons in a GroupBox.

Thanks guys, using Checked is much simpler (although perhaps not as clear). Still, works perfectly. Radio buttons is an option too.

Thanks for all the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.