C# 制定MSQ响应集合

C# 制定MSQ响应集合,c#,dictionary,radio-button,checkbox,C#,Dictionary,Radio Button,Checkbox,长话短说,作为实习的一部分,我正在准备一份考试申请表。我目前在候选者控制台模块上,该模块负责显示每个部分的问题并收集回答。响应收集基于提供的问题类型。如果问题是多项选择,候选控制台将使用自定义单选按钮的分组框显示该问题。例如-: 如果问题是“多选”,候选控制台将使用自定义复选框的组框显示该问题。例如-: 在IRC的帮助下,我成功地实现了MCQ响应收集。然而,按照同样的逻辑,我在实现MSQ响应集合时遇到了一个绊脚石 //This is in the QuestionDisplay class. /

长话短说,作为实习的一部分,我正在准备一份考试申请表。我目前在候选者控制台模块上,该模块负责显示每个部分的问题并收集回答。响应收集基于提供的问题类型。如果问题是多项选择,候选控制台将使用自定义单选按钮的分组框显示该问题。例如-:

如果问题是“多选”,候选控制台将使用自定义复选框的组框显示该问题。例如-:

在IRC的帮助下,我成功地实现了MCQ响应收集。然而,按照同样的逻辑,我在实现MSQ响应集合时遇到了一个绊脚石

//This is in the QuestionDisplay class.
//MCQ
public void AddOption_mcq(string optionText, bool arg, int QNo, int option)
{
     CustomRadio rb = new CustomRadio();
     rb.Text = optionText;
     rb.Location = new Point(3, 40 + grbOptions.Controls.Count * 30);
     rb.AutoSize = true;
     rb.Checked = arg;
     rb.QuestionNumber = QNo;
     rb.optionId = option;
     rb.CheckedChanged += delegate(Object sender, System.EventArgs e)
     {
           temp = sender as CustomRadio;
           if (!ResponseMCQ.ContainsKey(QNo)) //First time question is marked, ResponseMCQ is a <int,ButtonBase> dict.
           {
                ResponseMCQ.Add(QNo, temp);
           }
           else
                ResponseMCQ[QNo] = temp; //All other times
     };
     grbOptions.Controls.Add(rb); //grbOptions is a groupbox control.
}

//MSQ
public void AddOption_msq(string optionText, bool arg, int QNo, int option)
{
     CustomChecks cb = new CustomChecks();
     cb.Text = optionText;
     cb.Location = new Point(3, 40 + grbOptions.Controls.Count * 30);
     cb.AutoSize = true;
     cb.Checked = arg;
     cb.QuestionNumber = QNo;
     cb.optionId = option;
     cb.CheckedChanged += delegate(Object sender, System.EventArgs e)
     {
          temp = sender as CustomChecks;
          if(MSQs.Any())
              foreach (CustomChecks C in MSQs) //Clear elements from List if on a different question
              {
                   if (C.QuestionNumber != ((CustomChecks)temp).QuestionNumber)
                   {
                       IsDifferent = true;
                       break;
                   }
              }
          if (IsDifferent == true)
          {
               MSQs.Clear();
               IsDifferent = false;
          }
          if(!MSQs.Any(x => x.Text.Equals(optionText))) //Check if the checkbox already exists in the List
               MSQs.Add(temp);
          if (!ResponseMSQ.ContainsKey(QNo)) //First time the question is marked
          {
               ResponseMSQ.Add(QNo, MSQs);
          }
          else
               ResponseMSQ[QNo] = MSQs; //All other times
    };
    grbOptions.Controls.Add(cb);
    if (MSQs.Any())
    {
          foreach (CustomChecks C in MSQs)
          {
               foreach (CustomChecks D in grbOptions.Controls)
               {
                    if (D.Text.Equals(C.Text))
                    {
                        D.Checked = C.Checked;
                    }
               }
          }
    }
}

//In the main project, this is how I collect MCQ responses -:
temp = questionDisplay1.GetResponse; //questionDisplay1 is a QuestionDisplay object and GetResponse is a ButtonBase object, used to temporarily store the clicked radio button(CustomRadio object).
//MCQResponse is a Dictionary<int, ButtonBase> which stores <QuestionNumber, CustomRadio>.
if (QuesTypes[i].Equals("MCQ"))
{
         if (questionDisplay1.MCQResponse.TryGetValue(i, out temp) && questionDisplay1.MCQResponse[i].Text.Equals(s))
         {
               questionDisplay1.AddOption_mcq(s, true, i, optionId);
         }
         else
               questionDisplay1.AddOption_mcq(s, false, i, optionId);
}
这很有效。我调试以检查-:

正在存储响应:ResponseMSQ正在根据需要存储响应。 复选框按要求显示:由于末尾的双foreach,复选框的状态正确显示。
现在唯一需要关心的是double-foreach的运行时间,但是这个解决方案是有效的。如果有人知道比同时遍历两个列表更好的方法,他们可以在评论中发布。再次感谢

我的C/.NET有点生锈,但是在您的MSQ CheckedChanged委托中,这样做不是更容易吗:

temp = sender as CustomChecks;
if (!ResponseMSQ.ContainsKey(QNo)) 
{                             
    // First time, add an empty list
    ResponseMSQ.Add(QNo, new List<ButtonBase>());
} 
// At this point you're guaranteed there's something in the dictionary
// for your QNo, even if it's just an empty list.                                    
if (!ResponseMSQ[QNo].Any(x => x.Text.Equals(optionText)))
{
    // If the checkbox isn't already in the list, then add it
    ResponseMSQ[QNo].Add(temp); 
}
在MCQ和MSQ两种情况下,您都不会从这些字典中删除任何内容。有人永远不可能改变他们的回答吗?

注意回答mCq[QNo]=temp;线它有助于改变反应。无法取消选择单选按钮。我将尝试这种方法并公布结果。
temp = sender as CustomChecks;
if (!ResponseMSQ.ContainsKey(QNo)) 
{                             
    // First time, add an empty list
    ResponseMSQ.Add(QNo, new List<ButtonBase>());
} 
// At this point you're guaranteed there's something in the dictionary
// for your QNo, even if it's just an empty list.                                    
if (!ResponseMSQ[QNo].Any(x => x.Text.Equals(optionText)))
{
    // If the checkbox isn't already in the list, then add it
    ResponseMSQ[QNo].Add(temp); 
}