In our app that guesses people’s age (range), we want to force them to make a selection before moving on to the next question. We can do this by disabling the “Next” button until a selection is made. So these are the things we will do to accomplish that:
Each time a new set of options is displayed, we will disable the “Next” button.
But then when and how will we then enable it when we should allow it to be clicked again? We will watch (in code) for the selection of one of the RadioButton items. When that occurs, we will enable the “Next” button. Simple as that. So here’s the code to do it:
Set the “Next” button’s Enabled property to False by adding this code to the start of the LoadNextBatch() block of code (after “sequence++;”):
button1.Enabled = false;
The start of that code should now look like this (the new code is bolded):
private void LoadNextBatch()
{
sequence++;
button1.Enabled = false;
You can tell visually that the “Next” button is disabled (because it’s grayed out):

But if it’s never enabled again, we will not be able to use it to move on to the next set of questions. So we also need to add code to re-enable it. This we will do by “listening” to the RadioButton controls.
To do this, follow these steps:
1) In the Designer, highlight the first RadioButton by clicking on it. radiobutton1 should look like this after clicking it:

2) Move to the Events part of the Property pane in the lower right corner of Visual Studio by clicking the “Lightning” icon on it (rectangled below):

This will show you which events are available to write code for. The event we want is the one that is highlighted by default, CheckedChanged:

3) 2-click to the right of the CheckedChanged event (in the area rectangled above). This should open up the following code in the Form1 [Design] file:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
}
4) Write the following code in it (the new code is bolded):
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = (sender as RadioButton).Checked;
}
5) Rather than write the same code in the other two RadioButtons, hook up the other RadioButtons to this code we just wrote so that they use this same code for their CheckedChanged events, too. Do that by selecting the other two RadioButtons in turn, then selecting the drop down for their CheckedChanged event, and finally selecting the one we just wrote (rectangled below):

Now all three RadioButtons will use that same code – no matter which RadioButton is selected, it will “turn on” or re-enable the “Next” Button, so that it can be clicked.
There is one other thing that we need to do: the group of RadioButtons need to all be set to unchecked each time a new set of options displays. We handle that this way:
1) In the Designer, set the value of at least the first RadioButton’s AutoCheck property to false (rectangled below):

2) In code, set the values for all the RadioButtons back to True when the form is created. By default (without us having written it ourselves), that code is simply:
public Form1()
{
InitializeComponent();
}
Change it to the following (the new code is bolded):
public Form1()
{
InitializeComponent();
radioButton1.AutoCheck = true;
radioButton2.AutoCheck = true;
radioButton3.AutoCheck = true;
}
3) Set the Checked property for all RadioButtons to False in the LoadNextBatch() code (the new code is bolded):
private void LoadNextBatch()
{
sequence++;
button1.Enabled = false;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
. . .
It should now work fine. In the next step, we will calculate the final “score” (our guess of the user’s age range) and add something visual to display the result to the user.
Earth-shakingly Important Note: If you have a basic programming question (suitable to an audience of “Kids”), send it to idiolectable@gmail.com, specifying whether you would like your name and location used if it is printed in a future “Step” of this newsletter. A nickname is acceptable (the first “Letter to the Editor” of mine that was printed appeared in Rolling Stone magazine, back in the early 1970s, and I signed it “Sylvester” for some reason which I no longer remember). Also, it’s always interesting to see where people are from, so please provide your City or Town and the State it’s in, too (or Province, or whatever the region where you live is called).
The audio version of this Step can be found here: