Joel Bwana
Joel Bwana

Reputation: 43

Disable NumericUpdown C#

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

Upvotes: 0

Views: 401

Answers (2)

Mahmoud Ramadan
Mahmoud Ramadan

Reputation: 652

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);
   }      
}

Upvotes: 1

Caius Jard
Caius Jard

Reputation: 74595

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

Upvotes: 1

Related Questions