Example #1
0
        /// <summary>
        /// 为控件赋值
        /// </summary>
        /// <param name="control">控件</param>
        /// <param name="value">值</param>
        /// <returns>是否赋值成功</returns>
        /// <remarks>
        /// 创建人:qipengfei
        /// 创建日期:2017-08-25
        /// 修改人:
        /// 修改日期:yyyy-mm-dd
        /// 修改备注:无
        /// 版本:1.0
        /// </remarks>
        public static bool SetValue(Control control, object value)
        {
            if (control == null || value == null)
            {
                return(false);
            }

            try
            {
                // TODO 完成各种控件的赋值
                if (control is Label ||
                    control is Panel ||
                    control is TabControl ||
                    control is Button ||
                    control is LinkLabel ||
                    control is GroupBox)
                {
                    return(false);
                }// if
                else if (control is TextBox)
                {
                    ((TextBox)control).Text = value.ToString();
                    return(true);
                } //else if
                else if (control is RichTextBox)
                {
                    ((RichTextBox)control).Text = value.ToString();
                    return(true);
                }// else if
                else if (control is CheckBox)
                {
                    if (value is bool)
                    {
                        ((CheckBox)control).Checked = (bool)value;
                        return(true);
                    }// if
                    else
                    {
                        (control as CheckBox).Checked = Boolean.Parse(value.ToString());
                        return(true);
                    } // else
                }     // else if
                else if (control is DateTimePicker)
                {
                    if (value is string)
                    {
                        try
                        {
                            (control as DateTimePicker).Value = Convert.ToDateTime(value);
                            return(true);
                        }
                        catch
                        {
                            try
                            {
                                string   year = value.ToString().Substring(0, 4);
                                DateTime time = new DateTime(Convert.ToInt32(year), 1, 1);
                                (control as DateTimePicker).Value = time;

                                return(true);
                            }
                            catch
                            {
                                return(true);
                            }
                        }
                    }// if
                    else if (value is DateTime)
                    {
                        (control as DateTimePicker).Value = (DateTime)(value);
                        return(true);
                    }// else if
                    else if (value is long)
                    {
                        (control as DateTimePicker).Value = ObjectHelper.TimeStampToDateTime(Convert.ToInt64(value));
                        return(true);
                    } // else if
                }     // else if
                else if (control is ComboBox)
                {
                    ComboBox comboBox = control as ComboBox;
                    comboBox.SelectedValue = value;
                    if (comboBox.SelectedIndex == -1)
                    {
                        comboBox.Text = value.ToString();
                    } // if
                    return(true);
                }     // else if
                else if (control is ListBox)
                {
                    (control as ListBox).Text = value.ToString();
                    return(true);
                }// else if
                else if (control is CheckedListBox)
                {
                    (control as CheckedListBox).Text = value.ToString();
                    return(true);
                }// else if
                else if (control is NumericUpDown)
                {
                    (control as NumericUpDown).Value = (decimal)value;
                    return(true);
                }// else if

                control.Text = value.ToString();
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// 将控件的值映射到实体中对应的属性上
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="control"></param>
        /// <param name="obj"></param>
        /// <remarks>
        /// 创建人:qipengfei
        /// 创建日期:2017-08-24
        /// 修改人:
        /// 修改日期:yyyy-mm-dd
        /// 修改备注:无
        /// 版本:1.0
        /// </remarks>
        internal static T EvaluateRecuceive <T>(Control control, T obj)
            where T : class, new()
        {
            if (control is Label ||
                control is LinkLabel ||
                control is Button ||
                control is Panel ||
                control is GroupBox)
            {
                return(obj);
            } // if
            else
            {
                //控件名称
                string controlName = control.AccessibleName;
                //获取相应类型控件的值
                Object value = Evaluate(control);

                //设置值
                if (value != null && controlName != null)
                {
                    #region 设置值

                    if (obj is IDictionary <String, Object> )
                    {
                        if (((IDictionary <String, Object>)obj).ContainsKey(controlName))
                        {
                            ((IDictionary <String, Object>)obj)[controlName] = value;
                        }// if
                        else
                        {
                            ((IDictionary <String, Object>)obj).Add(controlName, value);
                        }// else
                    }
                    else if (obj is IDictionary)
                    {
                        if (((IDictionary)obj).Contains(controlName))
                        {
                            ((IDictionary)obj).Remove(controlName);
                        }// if
                        else
                        {
                            ((IDictionary)obj).Add(controlName, value);
                        } // else
                    }     // else if
                    else
                    {
                        Type         objType  = obj.GetType();
                        FastType     fastType = FastType.Get(objType);
                        FastProperty prop     = fastType.GetGetter(controlName);

                        if (prop != null)
                        {
                            try
                            {
                                //类型转换
                                value = ObjectHelper.ToType(prop.Type, value);
                                prop.SetValue(obj, value);
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("类型转换出错。\n字段名:" + controlName + ";字段值:" + value + "。\n" + ex.ToString());
                            }
                        } // if
                    }     // else
                    #endregion
                }
            }
            return(obj);
        }