public UCQuestion(tmo_questionnaire question) { InitializeComponent(); BackColor = Color.Transparent; //背景透明 Question = question; //题目赋值 //题目类型 1-判断题 2-选择题 3-多项选择题 4-填空题 5-问答题 if (question.q_type == 2) //选择题 { if (question.q_value_type.Equals("bool")) //bool类型 { _type = typeof(bool?); var tmp = new UcRadioGroup <bool?>(question.q_value, question.QuestionnaireResult.qr_result); tmp.SelectedChanged += (sender, args) => OnValueChanged(); _valueControls.Add(tmp); } else if (question.q_value_type.Equals("datetime")) //date类型 { _type = typeof(DateTime); var tmp = new UcRadioGroup <DateTime>(question.q_value, question.QuestionnaireResult.qr_result); tmp.SelectedChanged += (sender, args) => OnValueChanged(); _valueControls.Add(tmp); } else if (question.q_value_type.Equals("float")) //float类型 { _type = typeof(float); var tmp = new UcRadioGroup <float>(question.q_value, question.QuestionnaireResult.qr_result); tmp.SelectedChanged += (sender, args) => OnValueChanged(); _valueControls.Add(tmp); } else if (question.q_value_type.Equals("string")) //string类型 { _type = typeof(string); var tmp = new UcRadioGroup <string>(question.q_value, question.QuestionnaireResult.qr_result); tmp.SelectedChanged += (sender, args) => OnValueChanged(); _valueControls.Add(tmp); } } if (question.q_type == 3) //多选题 { if (question.q_value_type.Equals("int[]")) //int类型 { _type = typeof(int[]); var tmp = new CheckBoxGroup <int>(question.q_value, question.QuestionnaireResult.qr_result); tmp.SelectedChanged += (sender, args) => OnValueChanged(); _valueControls.Add(tmp); } } if (question.q_type == 4) //填空题 { if (question.q_value_type.Equals("float")) //float类型 { _type = typeof(float); } flowLayoutPanel1.FlowDirection = FlowDirection.LeftToRight; string[] values = TmoShare.GetValueFromJson <string[]>(Question.QuestionnaireResult.qr_result, false); if (values == null) { values = new[] { TmoShare.GetValueFromJson <string>(question.QuestionnaireResult.qr_result) } } ; int vindex = 0; string tmp = question.q_name; int index = -1; do { index = tmp.IndexOf('{'); string thisstr = index == -1 ? tmp : tmp.Substring(0, index); if (_valueControls.Count == 0) { lblQuestion.Text = string.Format("{0:00}. {1}", question.q_no, thisstr); } else { Label lbl = new Label(); lbl.Margin = new Padding(0, top, 0, 0); lbl.Text = thisstr; lbl.Font = lblQuestion.Font; lbl.AutoSize = true; flowLayoutPanel1.Controls.Add(lbl); } if (index != -1) { string laststr = tmp.Remove(0, index + 1); string lengthstr = new string(laststr.TakeWhile(x => TmoShare.IsNumeric(x)).ToArray()); laststr = laststr.Remove(0, lengthstr.Length + 1); string value = null; if (vindex < values.Length) { value = values[vindex]; vindex++; } TextBox txtBox = new TextBox(); txtBox.Width = Convert.ToInt32(lengthstr); txtBox.Text = value; txtBox.Margin = new Padding(0); txtBox.TextChanged += (sender, args) => OnValueChanged(); flowLayoutPanel1.Controls.Add(txtBox); _valueControls.Add(txtBox); tmp = laststr; } } while (index != -1); } else { lblQuestion.Text = string.Format("{0}. {1}", question.q_no.ToString("00"), question.q_name); flowLayoutPanel1.Controls.AddRange(_valueControls.ToArray()); } SetupLocation(); } /// <summary> /// 设置控件位置 /// </summary> void SetupLocation() { Width = width; //设置整体宽度 picIcon.Left = left; picIcon.Top = top + 2; //flowLayoutPanel1.Left = left + picIcon.Width + 3; flowLayoutPanel1.Width = flowLayoutPanel2.Width = width - left - picIcon.Width - 3; lblQuestion.Margin = lblTip.Margin = new Padding(0, top, 0, 0); //Size size = new Size(flowLayoutPanel1.Width, int.MaxValue); //lblQuestion.Size = TextRenderer.MeasureText(lblQuestion.Text, lblQuestion.Font, size); if (_valueControls.Any()) { if (flowLayoutPanel1.FlowDirection == FlowDirection.LeftToRight) { _valueControls.ForEach(x => x.Margin = new Padding(0, top, 0, 0)); Height = top + lblQuestion.Height + top; } else { _valueControls.ForEach(x => x.Margin = new Padding(25, 3, 0, 0)); Height = top + lblQuestion.Height + 3 + _valueControls.First().Height + top; } } else { Height = top + lblQuestion.Height + top; } //flowLayoutPanel1.Height = Height; }
private void InitValidate() { if (QuestionList.Any()) { #region 验证条件 var validates = QuestionList.Where(x => x.Question.q_is_validate && !string.IsNullOrWhiteSpace(x.Question.q_validate)); //找到有验证条件的 foreach (UCQuestion validate in validates) { tmo_questionnaire que = validate.Question; Dictionary <string, string> dic = TmoShare.GetValueFromJson <Dictionary <string, string> >(que.q_validate); if (dic == null || !dic.Any()) { continue; } foreach (KeyValuePair <string, string> keyValuePair in dic) { string type = keyValuePair.Key; string conditionStr = keyValuePair.Value; if (type == "calc") //自动计算 { List <string> qidList = new List <string>(); var macth = Regex.Match(conditionStr, @"\{.+?\}"); while (macth.Success) { string qid = macth.Value; if (!qidList.Contains(qid)) { qidList.Add(qid); } macth = macth.NextMatch(); } List <UCQuestion> tquelist = new List <UCQuestion>(); qidList.ForEach(x => { string qid = x.TrimStart('{').TrimEnd('}'); try { var tucque = QuestionList.First(y => y.Question.q_id == qid); tquelist.Add(tucque); } catch { } }); tquelist.ForEach(x => { x.Leave += ((sender, args) => { string str = conditionStr; foreach (UCQuestion y in tquelist) { if (!y.ValidateValue()) { return; //值类型验证失败 停止计算 } string tovale = y.GetValue().ToString(); if (string.IsNullOrWhiteSpace(tovale)) { return; } string replacekey = "{" + y.Question.q_id + "}"; str = str.Replace(replacekey, tovale); } object obj = TmoShare.CalcString(str); validate.SetValue(obj); }); }); } else if (type.StartsWith("enable") || type.StartsWith("disable")) //禁用选项 { string tqid = conditionStr; try { var tucque = QuestionList.First(y => y.Question.q_id == tqid); tucque.ValueChanged += (sender) => { object value = tucque.GetValue(); if (value is DateTime) { DateTime dt = (DateTime)value; if (dt != DateTime.MinValue && dt != new DateTime(9999, 12, 31)) { validate.Enabled = true; } else { validate.Enabled = false; validate.SetValue(null); } } if (value is bool) { if (type == "disable" || type == "enable") { bool enable = (bool)value; if (type == "disable") { enable = !enable; } if (!enable) { validate.SetValue(null); } validate.Enabled = enable; } } if (value is float) { if (type.Contains("|")) { float f = (float)value; var list = StringPlus.GetStrArray(StringPlus.GetStrArray(type, "-")[1], "|"); var flist = new List <float>(); list.ForEach(x => flist.Add(Convert.ToSingle(x))); bool enable = flist.Contains(f); if (type.StartsWith("disable")) { enable = !enable; } if (!enable) { validate.SetValue(null); } validate.Enabled = enable; } } }; tucque.OnValueChanged(); } catch { } } else if (type == "required") //必填验证 { validate.IsRequired = true; } } } #endregion #region 男女验证 var genders = QuestionList.Where(x => x.Question.q_gender != 0); //找到有性别限制的题目 foreach (UCQuestion gender in genders) { if (_user.gender != gender.Question.q_gender) { gender.Enabled = false; } } #endregion } }