继上篇文章winform 程序对界面上控件的统一控制【一】(赋值\清空\验证……) ,本篇文章将实现一个同样功能的Component(组件)。

  先看看组件的实现,如下所示:

public partial class CtrlValidation : Component
    {
        
public CtrlValidation()
        {
            InitializeComponent();
            typeCache 
= new TypeCache();
        }

        
public CtrlValidation(IContainer container)
        {
            container.Add(
this);

            InitializeComponent();
            typeCache 
= new TypeCache();
        }

        TypeCache typeCache;

        List
<RealSailing.UI.Utils.SetControl> ctrlCollect = new List<RealSailing.UI.Utils.SetControl>();
        [Description(
"控件集合")]
        [Editor(
typeof(DropEditor), typeof(UITypeEditor))]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        
public List<RealSailing.UI.Utils.SetControl> CtrlCollect
        {
            
get
            {
                
return ctrlCollect;
            }
            
set
            {
                ctrlCollect 
= value;
            }
        }

        
private object dataSource;
        [Category(
"CatData"), DefaultValue((string)null), RefreshProperties(RefreshProperties.Repaint),
        Description(
"DataGridDataSourceDescr"), AttributeProvider(typeof(IListSource))]
        
public object DataSource
        {
            
get
            {
                
return this.dataSource;
            }
            
set
            {
                
if (((value != null&& !(value is IList)) && !(value is IListSource))
                {
                    
throw new ArgumentException("BadDataSourceForComplexBinding");
                }
                
if ((this.dataSource == null|| !this.dataSource.Equals(value))
                {
                    
if (((value == null|| (value == Convert.DBNull)) && ((this.DataMember != null&& (this.DataMember.Length != 0)))
                    {
                        
this.dataSource = null;
                        
this.DataMember = "";
                    }
                    
else
                    {
                        
if (value != null)
                        {
                            
this.dataSource = value;
                        }
                    }
                }
            }
        }

        
private string dataMember;
        [DefaultValue((
string)null), Description("DataGridDataMemberDescr"),
        Category(
"CatData"),
        Editor(
"System.Windows.Forms.Design.DataMemberListEditor, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"typeof(UITypeEditor))]
        
public string DataMember
        {
            
get
            {
                
return this.dataMember;
            }
            
set
            {
                
if ((this.dataMember == null|| !this.dataMember.Equals(value))
                {
                    
this.dataMember = value;
                }
            }
        }

        
#region 提供方法
        
/// <summary>
        
/// 必填项是否为空,true表示不为空,FALSE表示为空
        
/// </summary>
        
/// <returns></returns>
        public bool CheckIsNull()
        {
            
foreach (RealSailing.UI.Utils.SetControl ctrl in ctrlCollect)
            {
                
object[] obj = new object[] { ctrl };
                TypeProcess _process 
= TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
                
bool editnull = _process.CheckNull();
                
if (!editnull)
                    
return editnull;
            }
            
return true;
        }
        
/// <summary>
        
/// 输入框的格式是否正确,true表示正确,FALSE表示不正确
        
/// </summary>
        
/// <returns></returns>
        public bool CheckIsFormate()
        {
            
foreach (RealSailing.UI.Utils.SetControl ctrl in ctrlCollect)
            {
                
if (ctrl.ControlName.GetType().Equals(typeof(DevExpress.XtraEditors.TextEdit)))
                {
                    
object[] obj = new object[] { ctrl };
                    TypeProcess _process 
= TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
                    
bool editformate = _process.CheckFormate();
                    
if (!editformate)
                        
return editformate;
                }
            }
            
return true;
        }
        
/// <summary>
        
/// 清空画面
        
/// </summary>
        public void SetCtrlClear()
        {
            ctrlCollect.ForEach(ctrl 
=>
            {
                
object[] obj = new object[] { ctrl };
                TypeProcess _process 
= TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
                _process.SetCtrlClear();
            });
        }
        
/// <summary>
        
/// 设置控件可用状态
        
/// </summary>
        
/// <param name="enable"></param>
        public void SetCtrlEnable(bool enable)
        {
            ctrlCollect.ForEach(ctrl 
=>
            {
                
object[] obj = new object[] { ctrl };
                TypeProcess _process 
= TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
                _process.SetCtrlEnable(enable);
            });
        }
        
/// <summary>
        
/// 设置控件默认值
        
/// </summary>
        public void SetCtrlDefault()
        {
            ctrlCollect.ForEach(ctrl 
=>
            {
                
object[] obj = new object[] { ctrl };
                TypeProcess _process 
= TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
                _process.SetCtrlDefault();
            });
        }
        
/// <summary>
        
/// 清除错误提示信息
        
/// </summary>
        public void ClearErrText()
        {
            ctrlCollect.ForEach(ctrl 
=>
            {
                
object[] obj = new object[] { ctrl };
                TypeProcess _process 
= TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
                _process.ClearErrText();
            });
        }
        
/// <summary>
        
/// 将数据行的值映射到文本编辑框中
        
/// </summary>
        
/// <param name="row"></param>
        public void LoadEditRowToText(DataRow row)
        {
            
if (row != null)
            {
                ctrlCollect.ForEach(ctrl 
=>
                {
                    
object[] obj = new object[] { ctrl };
                    TypeProcess _process 
= TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
                    _process.LoadToText(row);
                });
            }
        }
        
/// <summary>
        
/// 将编辑框中的数据赋给数据行
        
/// </summary>
        
/// <param name="row"></param>
        public void SetCtrlTextToEditRow(DataRow row)
        {
            
if (row != null)
            {
                ctrlCollect.ForEach(ctrl 
=>
                {
                    
object[] obj = new object[] { ctrl };
                    TypeProcess _process 
= TypeFactory.CreateType(ctrl.ControlName.GetType().Name, obj, ctrl.ToString(), typeCache);
                    _process.SetTextToRow(row);
                });
            }
        }
        
#endregion
    }

相关文章:

  • 2021-07-06
  • 2022-02-18
  • 2022-12-23
  • 2022-12-23
  • 2022-01-06
  • 2022-01-10
  • 2022-02-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-05
  • 2021-05-25
  • 2022-12-23
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案