Exemplo n.º 1
0
        public static Control CreateDate(Indicator indicator, string val, ErrorProvider indicatorErrors, List<DynamicContainer> controlList)
        {
            var container = new DynamicContainer { Indicator = indicator };
            var cntrl = new NullableDatePickerControl
            {
                Name = "dynamicDt" + indicator.Id.ToString(),
                Margin = new Padding(0, 5, 10, bottomPadding),
                ShowClear = !indicator.IsRequired,
                Value = DateTime.Now
            };
            // Add the Control to the DynamicContainer for reference
            container.Control = cntrl;
            container.IsValid = () =>
            {
                if (indicator.IsRequired && cntrl.GetValue() == DateTime.MinValue)
                {
                    indicatorErrors.SetError(cntrl, Translations.Required);
                    return false;
                }

                indicatorErrors.SetError(cntrl, "");
                return true;
            };
            cntrl.Validating += (s, e) => { container.IsValid(); };
            DateTime dt = new DateTime();
            if(DateTime.TryParseExact(val, "MM/dd/yyyy", 
    				CultureInfo.InvariantCulture, 
    				DateTimeStyles.None, 
    				out dt))
                cntrl.Value = dt;
            else
                cntrl.Value = DateTime.MinValue;

            container.GetValue = () => 
            {
                if (cntrl.GetValue() == DateTime.MinValue)
                    return "";
                else
                    return cntrl.GetValue().ToString("MM/dd/yyyy"); 
            };
            controlList.Add(container);
            return cntrl;
        }
        private Control CreateControl(Indicator indicator, string val)
        {
            var container = new DynamicContainer { Indicator = indicator };
            if (indicator.DataTypeId == (int)IndicatorDataType.Date)
            {
                DateTime d;
                var cntrl = new NullableDatePickerControl
                {
                    Name = "dynamicDt" + indicator.Id.ToString(),
                    ShowClear = true
                };
                container.IsValid = () =>
                {
                    if (indicator.IsRequired)
                    {
                        if (cntrl.Text == "" || cntrl.Text == null)
                        {
                            indicatorErrors.SetError(cntrl, Translations.Required);
                            return false;
                        }
                        else if (!DateTime.TryParse(cntrl.Text, out d))
                        {
                            indicatorErrors.SetError(cntrl, Translations.MustBeDate);
                            return false;
                        }
                        else
                            indicatorErrors.SetError(cntrl, "");
                    }
                    return true;
                };
                cntrl.Validating += (s, e) => { container.IsValid(); };
                DateTime dt = new DateTime();
                if (DateTime.TryParseExact(val, "MM/dd/yyyy",
                        CultureInfo.InvariantCulture,
                        DateTimeStyles.None,
                        out dt))
                    cntrl.Value = dt;
                else
                    cntrl.Value = DateTime.MinValue;

                container.GetValue = () =>
                {
                    if (cntrl.GetValue() == DateTime.MinValue)
                        return "";
                    else
                        return cntrl.GetValue().ToString("MM/dd/yyyy");
                };
                controlList.Add(container);
                return cntrl;
            }
            else if (indicator.DataTypeId == (int)IndicatorDataType.Number)
            {
                double d = 0;
                var cntrl = new TextBox { Name = "dynamicNum" + indicator.Id.ToString(), Text = val };
                container.IsValid = () =>
                {
                    if (indicator.IsRequired)
                    {
                        if (cntrl.Text == "" || cntrl.Text == null)
                        {
                            indicatorErrors.SetError(cntrl, Translations.Required);
                            return false;
                        }
                        else if (!Double.TryParse(cntrl.Text, out d))
                        {
                            indicatorErrors.SetError(cntrl, Translations.MustBeNumber);
                            return false;
                        }
                        else
                            indicatorErrors.SetError(cntrl, "");
                    }
                    return true;
                };
                cntrl.Validating += (s, e) => { container.IsValid(); };
                
                container.GetValue = () => { return cntrl.Text; };
                controlList.Add(container);
                return cntrl;
            }
            else if (indicator.DataTypeId == (int)IndicatorDataType.YesNo)
            {
                var cntrl = new CheckBox { Name = "dynamicChk" + indicator.Id.ToString() };
                container.IsValid = () => { return true; };
                bool isChecked = false;
                if (Boolean.TryParse(val, out isChecked))
                    cntrl.Checked = isChecked;

                container.GetValue = () => { return Convert.ToInt32(cntrl.Checked).ToString(); };
                controlList.Add(container);
                return cntrl;
            }
            else
            {
                var cntrl = new TextBox { Name = "dynamicTxt" + indicator.Id.ToString(), Text = val };
                container.IsValid = () => {
                    if (indicator.IsRequired)
                    {
                        if (cntrl.Text == "" || cntrl.Text == null)
                        {
                            indicatorErrors.SetError(cntrl, Translations.Required);
                            return false;
                        }
                        else
                            indicatorErrors.SetError(cntrl, "");
                    }
                    return true;
                };
                cntrl.Validating += (s, e) => { container.IsValid(); };

                container.GetValue = () => { return cntrl.Text; };
                controlList.Add(container);
                return cntrl;
            }
        }