0

I'm a Beginner. I'm using a CheckBoxList for my Quiz. I want Values 1-4 to be the Correct answer and 5 to be Incorrect. Then have the results display OnClick through my Results label. Such that:

  1. Fashion & Design Club (Correct)
  2. Fishing Club (Correct)
  3. Dance Club (Correct)
  4. Art Club (Correct)
  5. NONE (Incorrect)

No matter what, if 1-4 is included with 5 I want the answer to be Incorrect. How do I do that? I tried using an If statement I'm finding that this clearly isn't going to work.

My Code:

    protected void SubmitButton_Click(object sender, ImageClickEventArgs e)
    {            
       if (CheckBoxList1.SelectedValue == "1")
        {
            Results.Text += " <br> Question 4 is Correct. <br>";
        }
        else if (CheckBoxList1.SelectedValue == "5")
        {
            Results.Text += " Question 4 is Incorrect. <br>";
        }
    }
KPIT
  • 27
  • 1
  • 1
  • 5
  • https://stackoverflow.com/questions/18924147/how-to-get-values-of-selected-items-in-checkboxlist-with-foreach-in-asp-net-c – Chetan Mar 05 '19 at 01:11
  • @Kerith Paschall, check the answer that I just posted. You can set the label text in click event after the logic for answer being correct or not. – Sunil Mar 05 '19 at 04:20

2 Answers2

0

Use the following method to decide if 5 is selected or not. Call this method from your button on click event.

In logic below, if 5 is selected with or without others then answer is wrong, but if any of 1 to 4 is/are selected then answer is correct.

protected void SubmitButton_Click(object sender, ImageClickEventArgs e)
{
    bool is1To4Selected =  false;
    for (int i=0; i < CheckBoxList1.Items.Count; i++)
    {
      if (CheckBoxList1.Items[i].Selected  && CheckBoxList1.Items[i].Value == "5")
      {
       is1To4Selected = false;
       break;
      } else if(CheckBoxList1.Items[i].Selected) 
      {
        is1To4Selected = true;
      }
   }

   if(is1To4Selected)
   {
     Results.Text += " <br> Question 4 is Correct. <br>";
   } else {
      Results.Text += " Question 4 is Incorrect. <br>";
   }
}
Sunil
  • 20,653
  • 28
  • 112
  • 197
  • Error: Since 'Project4.SubmitButton_Click(object, ImageClickEventArgs)' returns void, a return keyword must not be followed by an object expression – KPIT Mar 05 '19 at 05:29
  • Beautiful. It works perfect. Thank you so much for your help. – KPIT Mar 05 '19 at 05:42
0

The following code checks that whether "NONE" is present in the list of checked Items then answer is False. If not then at least one of the other boxes should be checked in order for answer to be true

public bool isCorrect() {
    if (checkedListBox1.CheckedItems.Contains("NONE")) {
        return false; //wrong answer is selected
    }
    else if (checkedListBox1.CheckedIndices.Count > 0) {
        return true; //at least one correct answer is selected
    }
    else
    return false; //no option is selected
}
karel
  • 5,489
  • 46
  • 45
  • 50
Zain Arshad
  • 1,885
  • 1
  • 11
  • 26