コード例 #1
0
        public List <Answer> GetAnswer()
        {
            Dictionary <string, Answer> result = new Dictionary <string, Answer>();

            //找出所有 tag 值為 Question type 的控制項,
            foreach (Control ctrl in this.allQControls.Values)
            {
                Question q = ctrl.Tag as Question;
                if (q != null)
                {
                    if (!result.ContainsKey(q.GetQuestionName()))
                    {
                        result.Add(q.GetQuestionName(), new Answer());
                    }
                    Answer ans = result[q.GetQuestionName()];
                    ans.SetName(q.GetQuestionName());

                    if (ctrl is CheckBox)   //如果是 checkbox :
                    {
                        CheckBox chk = (CheckBox)ctrl;

                        if (chk.Checked)
                        {
                            //這是複選題
                            AnswerItem       theAi            = new AnswerItem();
                            string           ctrlName         = ctrl.Name;
                            QuestionListItem questionListItem = q.GetListItemByLable(ctrlName);
                            theAi.SetValueName(ctrlName);
                            if (questionListItem != null)
                            {
                                if (questionListItem.HasText)  //有 remark
                                {
                                    string remarkCtrlName = ctrlName + "_remark";
                                    if (this.allQControls.ContainsKey(remarkCtrlName))
                                    {
                                        theAi.SetValueRemark(this.allQControls[remarkCtrlName].Text);
                                    }
                                    else
                                    {
                                        theAi.SetValueRemark("");
                                    }
                                }
                            }
                            ans.AddAnswerItem(theAi);
                        } //end of (chk.Checked)
                    }
                    else if (ctrl is DataGridView)
                    {
                        DataGridView dg = (DataGridView)ctrl;
                        Dictionary <string, string> value = new Dictionary <string, string>();
                        foreach (DataGridViewRow row in dg.Rows)
                        {
                            if (!row.IsNewRow)
                            {
                                AnswerItem ai = new AnswerItem();
                                Dictionary <string, string> itemContents = new Dictionary <string, string>();
                                foreach (GridColumn gc in q.GetColumns())
                                {
                                    itemContents.Add(gc.GetName(), (row.Cells[gc.GetName()].Value == null) ? "" : row.Cells[gc.GetName()].Value.ToString());
                                }
                                ai.SetContent(itemContents);
                                ans.GetAnswerItems().Add(ai);
                            }
                        }
                    }
                    else   //視為單選題
                    {
                        if (q.GetQuestionType().ToLower() == "textboxdropdown")
                        {
                            ctrl.Text = ctrl.Text.Trim();
                            string[] strArr = ctrl.Text.Split(',');
                            foreach (string str in strArr)
                            {
                                if (!string.IsNullOrEmpty(str))
                                {
                                    XmlElement elm = new XmlDocument().CreateElement("Item");
                                    elm.SetAttribute("value", str.Trim());
                                    AnswerItem ai = new AnswerItem(elm);
                                    ans.AddAnswerItem(ai);
                                }
                            }
                        }
                        else
                        {
                            ans.SetValue(ctrl.Text);
                        }
                    }
                }
            }


            return(result.Values.ToList <Answer>());
        }
コード例 #2
0
        /**
         * <Answers label="本人概況"> <!-- 對應到 Subject的 label -->
         *          <Ans name="AAA12345670" value=”AB”></Ans>
         *          <Ans name="AAA12345671" value=”基督教”></Ans>
         *          <Ans name="AAA12345678" >
         *                  <Item value=”近視” />
         *          <Item value=”其它” remark=”蛀牙” />
         *      </Ans>
         *          <Ans name="AAA12345679">
         *          <Item value=”氣喘” />
         *          <Item value=”其它” remark=”身心症” />
         *      </Ans>
         *  </Answers>
         * */
        public void SetAnswer(XmlElement answers)
        {
            this.answers = new Answers(answers);
            foreach (Answer ans in this.answers.GetAllAnswers())
            {
                Question q = this.questionGroup.GetQuestionByName(ans.GetName()); //根據解答名稱找到該題目
                if (q != null)                                                    //answer 是一整包 subject 裡所有題目的答案,而此 questionGroup中只包含其中一部分。
                {
                    if (q.GetQuestionType().ToLower() == "checkbox")              //checkbox 為複選,
                    {
                        foreach (AnswerItem ai in ans.GetAnswerItems())
                        {
                            //找到相關的 checkbox,並設定值為 checked
                            CheckBox chk = null;
                            if (this.allQControls.ContainsKey(ai.GetValueName()))
                            {
                                chk         = (CheckBox)this.allQControls[ai.GetValueName()];
                                chk.Checked = true;
                                QuestionListItem questionListItem = q.GetListItemByLable(ai.GetValueName());
                                if (questionListItem != null)
                                {
                                    if (questionListItem.HasText)
                                    {
                                        TextBox txt = (TextBox)this.allQControls[chk.Name + "_remark"];
                                        txt.Text = ai.GetValueRemark();
                                    }
                                }
                            }
                        }
                    }
                    else if (q.GetQuestionType().ToLower() == "grid")
                    {
                        DataGridView dg = (DataGridView)this.allQControls[ans.GetName()];
                        dg.Rows.Clear();

                        foreach (AnswerItem ai in ans.GetAnswerItems())
                        {
                            //每筆 AnswerItem 是一筆 Row
                            List <object> cellValues             = new List <object>();
                            Dictionary <string, string> contents = ai.GetContents();
                            foreach (GridColumn gc in q.GetColumns())
                            {
                                if (contents.ContainsKey(gc.GetName()))
                                {
                                    cellValues.Add(contents[gc.GetName()]);
                                }
                            } //end of foreach GridColumn
                            dg.Rows.Add(cellValues.ToArray <object>());
                        }     //end of foreach AnswerItem
                    }
                    else if (q.GetQuestionType().ToLower() == "textboxdropdown")
                    {
                        Control ctl = this.allQControls[ans.GetName()];
                        //Question question = (Question)ctl.Tag;
                        List <string> strList = new List <string> ();
                        foreach (AnswerItem ai in ans.GetAnswerItems())
                        {
                            strList.Add(ai.GetValueName());
                        }
                        ctl.Text = string.Join(",", strList.ToArray());
                    }
                    else  //for combobox and textbox
                    {
                        if (this.allQControls.ContainsKey(ans.GetName()))
                        {
                            Control ctl = this.allQControls[ans.GetName()];
                            //Question question = (Question)ctl.Tag;
                            ctl.Text = ans.GetValue();
                        }
                    }
                }   //end of "q is not null"
            }
        }