private static bool ValidateUserInput(Control control, Template.TemplateInfo.UserVariable uv) { string error = string.Empty; /* object input = GetUserInput(control, uv); * if (input == null) return true; * * try { Convert.ChangeType(input, uv.ParameterType); } catch (Exception exception) { error = exception.Message; } * * if (error == string.Empty && (uv.MinMax.Item1 != double.MinValue || uv.MinMax.Item2 != double.MaxValue)) * { * try * { * double dInput = Convert.ToDouble(input); * if (dInput < uv.MinMax.Item1 || dInput > uv.MinMax.Item2) * error = uv.MinMax.Item1 == double.MinValue ? "Value must not be greater than " + uv.MinMax.Item2.ToString() * : (uv.MinMax.Item2 == double.MaxValue ? "Value must not be smaller than " + uv.MinMax.Item1.ToString() * : string.Format("Value must be between {0} and {1}", uv.MinMax.Item1, uv.MinMax.Item2)); * } * catch { } * } */ if (error != string.Empty) { MessageBox.Show(uv.name + ":" + Environment.NewLine + error); return(false); } return(true); }
private void btnOK_Click(object sender, System.EventArgs e) { userVariables = new List <Template.TemplateInfo.UserVariable>(); foreach (Control control in panContent.Controls) { if (control.Tag == null || control.Tag is Template.TemplateInfo.UserVariable == false) { continue; } Template.TemplateInfo.UserVariable uv = control.Tag as Template.TemplateInfo.UserVariable; object input = GetUserInput(control, uv); if (input == null) { //if (!uv.Compulsory) continue; MessageBox.Show("This input is compulsory. Please indicate a value."); control.Focus(); return; } (control.Tag as Template.TemplateInfo.UserVariable).value = input.ToString(); if (control is ComboBox) { (control.Tag as Template.TemplateInfo.UserVariable).description = (control as ComboBox).SelectedItem.ToString(); } userVariables.Add(control.Tag as Template.TemplateInfo.UserVariable); } if (userVariables.Count == 0) { userVariables = null; } DialogResult = DialogResult.OK; Close(); }
private void inputBox_Validating(object sender, CancelEventArgs e) { try { Control control = sender as Control; Template.TemplateInfo.UserVariable uv = control.Tag as Template.TemplateInfo.UserVariable; e.Cancel = !ValidateUserInput(control, uv); } catch { } }
private static object GetUserInput(Control control, Template.TemplateInfo.UserVariable uv) { if (control is TextBox /* || uiDef.AllowFreeTyping*/) { return(control.Text == string.Empty ? null : control.Text); } ComboBox comboBox = control as ComboBox; return(comboBox.SelectedItem != null && uv.comboItems.Count(x => x.name == comboBox.SelectedItem.ToString()) > 0 ? uv.comboItems.Where(x => x.name == comboBox.SelectedItem.ToString()).Select(x => x.value).First() : null); }