0

The student is back! I am trying to self-teach C#, so please pardon my simple but many questions. I appreciate you all.

I am working on a quiz app.What I want but cant seem to achieve is that when "Testing mode" (radio button) is selected, "Number of questions" need to be grayed out.Otherwise, student can select number of questions to attempt. Test Options

Here is my code

private void rdotesting_CheckedChanged(object sender, EventArgs e)
    {

        if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
        {
            MessageBox.Show("Click 'Start' to continue..");
            btnclose.Hide();
        }
            else 
            {
              MessageBox.Show("You Must select an option to continue.");
            
            }
        
    }
            //if testing mode, dissable number of questions ,and also the 'Close' button
Joel Bwana
  • 43
  • 6

2 Answers2

1

Like this - see comments

private void rdotesting_CheckedChanged(object sender, EventArgs e)
{

    //this event fires when rdotesting is checked or when it is unchecked (change)
    //set the enabled state of the nud/button to th opposite of the checked state
    //ie when checked = true then enabled = false
    numberQsNUD.Enabled = !rdotesting.Checked;
    closeButton.Enabled = !rdotesting.Checked;

    //if not in test mode, exit to stop the message showing every time
    if(!rdotesting.Checked)
      return;

    

    if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
      rdotesting.Checked = false; //user said no; turn off test mode
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • This was very helpful. Just additional question...I want the Start Button to be disabled until a selection (Learning or Testing mode) is made. How do i go about that? – Joel Bwana Jul 08 '20 at 15:27
  • Set it as enabled=false in its properties in the forms designer then have two event handlers (one on each radio button - you've already got one on rdotesting so just add another one) and in each handler set startButton.Enabled = true... That way it doesn't matter which thing the user clicks; one or the other is gonna cause startBUtton.Enabled = true – Caius Jard Jul 08 '20 at 16:00
  • Worked beautifully. Thanks Caius – Joel Bwana Jul 09 '20 at 01:21
1

simply you can try this code...

private void rdotesting_CheckedChanged(object sender, EventArgs e) {
  if(rdotesting.Checked) {
     numberQsNUD.Enabled = false;
     closeButton.Enabled = false;
  } else {
     numberQsNUD.Enabled = true;
     closeButton.Enabled = true;
   }      
}

or you can customize this via using this code...

private void EnableComponent(bool check) {
     numberQsNUD.Enabled = check;
     closeButton.Enabled = check;
}

private void rdotesting_CheckedChanged(object sender, EventArgs e) {
  if(rdotesting.Checked) {
    EnableComponent(false);
  } else {
    EnableComponent(true);
   }      
}
  • This was very helpful. Just additional question...I want the Start Button to be disabled until a selection (Learning or Testing mode) is made. How do i go about that? – Joel Bwana Jul 08 '20 at 15:27