I'm doing a program which consists on a Test, with the following structure:

  1. Question 1
    rdioBtn 1 rdioBtn2 rdioBtn 3

  2. Question 2
    rdioBtn 4 rdio Btn 5 rdioBtn6

And so on...

Each question is inside of a groupbox.
What I wanna do, is to disable my "Next" button, until every question has one radio button checked.
Any idea how?

Recommended Answers

All 7 Replies

What you will do is set the button to be disabled by default, so when the questions load, it will be disabled already.

There are a number of ways you can handle this, but you could handle the CheckChanged event for RadioButtons and then actually check each group on the form. When at least one is checked in each group, then you will set button1.Enabled = true

I would recommend you give it a try and post your code if you run into any issues. Below is a link to how the check_changed event works.

Click Here

jbrock31 has to right idea.

Have a loop that checks whether the user has answered a particular question and then set the rdioBtn.Enabled property to either true or false depending on what outcome you wish for.

The loop can be accessed either as a constant loop that runs all the time whilst the application is running:

while(true)
{
    if(question_one.Completed)
    {
        rdioBtn3.Enabled = true;
    }
    // Other code if needed.
}

Bare in mind, this can cause issues with Threads, so be sure to sort out a threading solution for example each thread runs the a loop to check for question completion.

One note about GeissT's submission, if used ensure you use a Thread.Sleep() in that loop to avoid just using your CPU constantly on that loop.

Thanks for that MikeyIsMe, completely forgot about that.

No worries

And you have to be aware of something else, by default radioButtons are meant to have one radio (1st one normally) checked. They are meant that one of them (from a group) is always checked.
To avoid this, you have to uncheck the 1st one.

I would make the group-box with the radio buttons into a custom user control, then give it a property "IsAnswered" or something similar

public bool IsAnswered
{
    get
    {
        return (this.Controls.OfType<RadioButton>().Any(x => x.Checked));
    }
}

//Then in the main form, in your validation function (or event or whatever)
btnNext.Enabled = this.Controls.OfType<MyCustomQuestionBox>().All(x => x.IsAnswered);

You don't really need to make the custom control, but it cleans things up a bit. Otherwise you will need to go through each groupbox, then go through each radio button:

btnNext.Enabled = this.Controls.OfType<GroupBox>().All(x => x.Controls.OfType<RadioButton>().Any(y => y.Checked));

Of course there's a lot of other ways to do this too.

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.