SVXX

Response Collection

May 5th, 2013
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.18 KB | None | 0 0
  1. //This is in the QuestionDisplay class.
  2. //MCQ
  3. public void AddOption_mcq(string optionText, bool arg, int QNo, int option)
  4. {
  5.      CustomRadio rb = new CustomRadio();
  6.      rb.Text = optionText;
  7.      rb.Location = new Point(3, 40 + grbOptions.Controls.Count * 30);
  8.      rb.AutoSize = true;
  9.      rb.Checked = arg;
  10.      rb.QuestionNumber = QNo;
  11.      rb.optionId = option;
  12.      rb.CheckedChanged += delegate(Object sender, System.EventArgs e)
  13.      {
  14.            temp = sender as CustomRadio;
  15.            if (!ResponseMCQ.ContainsKey(QNo)) //First time question is marked, ResponseMCQ is a <int,ButtonBase> dict.
  16.            {
  17.                 ResponseMCQ.Add(QNo, temp);
  18.            }
  19.            else
  20.                 ResponseMCQ[QNo] = temp; //All other times
  21.      };
  22.      grbOptions.Controls.Add(rb); //grbOptions is a groupbox control.
  23. }
  24.  
  25. //MSQ
  26. public void AddOption_msq(string optionText, bool arg, int QNo, int option)
  27. {
  28.      CustomChecks cb = new CustomChecks();
  29.      cb.Text = optionText;
  30.      cb.Location = new Point(3, 40 + grbOptions.Controls.Count * 30);
  31.      cb.AutoSize = true;
  32.      cb.Checked = arg;
  33.      cb.QuestionNumber = QNo;
  34.      cb.optionId = option;
  35.      cb.CheckedChanged += delegate(Object sender, System.EventArgs e)
  36.      {
  37.           temp = sender as CustomChecks;
  38.           if(MSQs.Any())
  39.               foreach (CustomChecks C in MSQs) //Clear elements from List if on a different question
  40.               {
  41.                    if (C.QuestionNumber != ((CustomChecks)temp).QuestionNumber)
  42.                    {
  43.                        IsDifferent = true;
  44.                         break;
  45.                    }
  46.               }
  47.           if (IsDifferent == true)
  48.           {
  49.                MSQs.Clear();
  50.                IsDifferent = false;
  51.           }
  52.           if(!MSQs.Any(x => x.Text.Equals(optionText))) //Check if the checkbox already exists in the List
  53.                MSQs.Add(temp);
  54.           if (!ResponseMSQ.ContainsKey(QNo)) //First time the question is marked
  55.           {
  56.                ResponseMSQ.Add(QNo, MSQs);
  57.           }
  58.           else
  59.                ResponseMSQ[QNo] = MSQs; //All other times
  60.     };
  61.     grbOptions.Controls.Add(cb);
  62.     if (MSQs.Any())
  63.     {
  64.           foreach (CustomChecks C in MSQs)
  65.           {
  66.                foreach (CustomChecks D in grbOptions.Controls)
  67.                {
  68.                     if (D.Text.Equals(C.Text))
  69.                     {
  70.                         D.Checked = C.Checked;
  71.                     }
  72.                }
  73.           }
  74.     }
  75. }
  76.  
  77. //In the main project, this is how I collect MCQ responses -:
  78. temp = questionDisplay1.GetResponse; //questionDisplay1 is a QuestionDisplay object and GetResponse is a ButtonBase object, used to temporarily store the clicked radio button(CustomRadio object).
  79. //MCQResponse is a Dictionary<int, ButtonBase> which stores <QuestionNumber, CustomRadio>.
  80. if (QuesTypes[i].Equals("MCQ"))
  81. {
  82.          if (questionDisplay1.MCQResponse.TryGetValue(i, out temp) && questionDisplay1.MCQResponse[i].Text.Equals(s))
  83.          {
  84.                questionDisplay1.AddOption_mcq(s, true, i, optionId);
  85.          }
  86.          else
  87.                questionDisplay1.AddOption_mcq(s, false, i, optionId);
  88. }
Advertisement
Add Comment
Please, Sign In to add comment