SVXX

StackOverflow

May 6th, 2013
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.37 KB | None | 0 0
  1. Hello all.
  2.  
  3. Long story short, I'm working on an exam-taking application as part of my internship. I'm currently on the candidate console module, which is responsible for displaying questions per section and collecting responses. Response collection is based on the type of question that is offered. If the question is multiple choice, the candidate console displays it using a groupbox of (custom) radio buttons. For example -:
  4.  
  5. ![MCQ Response](http://s1.postimg.org/3slel5h57/MCQResponse.png)
  6.  
  7. If the question is multiple select, the candidate console displays it using a groupbox of (custom) checkboxes. For example -:
  8.  
  9. ![MSQ Response](http://s1.postimg.org/5lob9h2bv/MSQResponse.png)
  10.  
  11. I've been able to successfully implement MCQ response collection with some help from IRC. However, following the same logic, I've hit a stumbling block in the implementation of MSQ response collection.
  12.  
  13. //This is in the QuestionDisplay class.
  14. //MCQ
  15. public void AddOption_mcq(string optionText, bool arg, int QNo, int option)
  16. {
  17. CustomRadio rb = new CustomRadio();
  18. rb.Text = optionText;
  19. rb.Location = new Point(3, 40 + grbOptions.Controls.Count * 30);
  20. rb.AutoSize = true;
  21. rb.Checked = arg;
  22. rb.QuestionNumber = QNo;
  23. rb.optionId = option;
  24. rb.CheckedChanged += delegate(Object sender, System.EventArgs e)
  25. {
  26. temp = sender as CustomRadio;
  27. if (!ResponseMCQ.ContainsKey(QNo)) //First time question is marked, ResponseMCQ is a <int,ButtonBase> dict.
  28. {
  29. ResponseMCQ.Add(QNo, temp);
  30. }
  31. else
  32. ResponseMCQ[QNo] = temp; //All other times
  33. };
  34. grbOptions.Controls.Add(rb); //grbOptions is a groupbox control.
  35. }
  36.  
  37. //MSQ
  38. public void AddOption_msq(string optionText, bool arg, int QNo, int option)
  39. {
  40. CustomChecks cb = new CustomChecks();
  41. cb.Text = optionText;
  42. cb.Location = new Point(3, 40 + grbOptions.Controls.Count * 30);
  43. cb.AutoSize = true;
  44. cb.Checked = arg;
  45. cb.QuestionNumber = QNo;
  46. cb.optionId = option;
  47. cb.CheckedChanged += delegate(Object sender, System.EventArgs e)
  48. {
  49. temp = sender as CustomChecks;
  50. if(MSQs.Any())
  51. foreach (CustomChecks C in MSQs) //Clear elements from List if on a different question
  52. {
  53. if (C.QuestionNumber != ((CustomChecks)temp).QuestionNumber)
  54. {
  55. IsDifferent = true;
  56. break;
  57. }
  58. }
  59. if (IsDifferent == true)
  60. {
  61. MSQs.Clear();
  62. IsDifferent = false;
  63. }
  64. if(!MSQs.Any(x => x.Text.Equals(optionText))) //Check if the checkbox already exists in the List
  65. MSQs.Add(temp);
  66. if (!ResponseMSQ.ContainsKey(QNo)) //First time the question is marked
  67. {
  68. ResponseMSQ.Add(QNo, MSQs);
  69. }
  70. else
  71. ResponseMSQ[QNo] = MSQs; //All other times
  72. };
  73. grbOptions.Controls.Add(cb);
  74. if (MSQs.Any())
  75. {
  76. foreach (CustomChecks C in MSQs)
  77. {
  78. foreach (CustomChecks D in grbOptions.Controls)
  79. {
  80. if (D.Text.Equals(C.Text))
  81. {
  82. D.Checked = C.Checked;
  83. }
  84. }
  85. }
  86. }
  87. }
  88.  
  89. //In the main project, this is how I collect MCQ responses -:
  90. temp = questionDisplay1.GetResponse; //questionDisplay1 is a QuestionDisplay object and GetResponse is a ButtonBase object, used to temporarily store the clicked radio button(CustomRadio object).
  91. //MCQResponse is a Dictionary<int, ButtonBase> which stores <QuestionNumber, CustomRadio>.
  92. if (QuesTypes[i].Equals("MCQ"))
  93. {
  94. if (questionDisplay1.MCQResponse.TryGetValue(i, out temp) && questionDisplay1.MCQResponse[i].Text.Equals(s))
  95. {
  96. questionDisplay1.AddOption_mcq(s, true, i, optionId);
  97. }
  98. else
  99. questionDisplay1.AddOption_mcq(s, false, i, optionId);
  100. }
  101.  
  102.  
  103. `MCQResponse` and `MSQResponse` are of type `Dictionary<int, ButtonBase>` and `Dictionary<int, List<ButtonBase>>` respectively. They store data according to a QuestionNumber:Object mapping, where Object may be `CustomRadio` or `CustomChecks`.
  104.  
  105. For MCQs, I use a temporary ButtonBase object, `temp`, to store the CustomRadio object when it is clicked (CheckedChanged event). For the first time the question is marked, the dictionary is checked. If not present, the entry is added. For all other times, the entry is simply edited.
  106.  
  107. Now for MSQs, I'm trying to follow a similar logic but am screwing up somewhere. I use a temporary List<ButtonBase> object, `MSQs`, to store multiple CustomChecks responses. In the first if case, I clear the List if I am on a different question from the one in which I clicked a checkbox. Next, I check if the checkbox is not already present in the List. If not present, I add it. Lastly, I use a similar logic as in MCQ response collection.
  108.  
  109. The `foreach` loop at the end was added for the sake of displaying the CustomChecks elements properly. I ought to be using MSQResponse for that purpose, but I'm not able to figure out how. Here are the main problems I'm facing -:
  110.  
  111. - MSQ responses are persistently displayed only for one question. If I select checkboxes in another question, the previous ones are not displayed. I have to build code in the main project similar to how I've done for MCQs, and am not able to figure it out for that purpose.
  112. - The use of `MSQResponse`. I'm not using it properly for sure. There has to be a more efficient way to collect MSQ responses using this dictionary.
  113. - The double foreach loop at the end of the MSQ method is quite expensive. I have heard of Zip operations in .NET 4, but I'm using .NET 3.5 on strict instructions by my project in-charge. It is only for the purpose of displaying persistently, but it works only for one question. If I could do away with it altogether, I'd be glad.
  114.  
  115. Any help will be greatly appreciated!
Advertisement
Add Comment
Please, Sign In to add comment