95992828九五至尊2

好日子嘎啦通用权限管理系列解读及重构升华,窗体已毕增删改查

一月 24th, 2019  |  882828九五至尊手机版

吉日嘎拉,一个只顾于通用权限管理种类的支出狂热者,在乐乎是一个有争辩的人选,但是从其文章多少及内容介绍,专注肯定不是浪得虚名,一个人把东西做的令人瞩目,也就象征越来越多的投入及考虑,可以用作后来者越多的借鉴。

参考博客下方:http://www.cnblogs.com/wuhuacong/archive/2010/05/31/1748579.html

自身获其交情赠送一套源码,用作深远剖析及提炼使用,初一接触,整个连串架构依旧相比庞大,如下所示。 

对于一般常用到的编纂数据、新增多少窗体,分开了三个例外的窗体进行拍卖,而且由于BaseForm窗体没有对通用的函数举办进一步的用空想来安慰自己,因而,编辑及新增窗体多了很多重复累赘的代码,其实可以把新增、编辑合并一个窗体,然后按照新增、编辑二种分歧的规格举办拍卖即可。

882828九五至尊手机版 1 

出于BaseForm一般需求在半数以上的窗体中,而新增编制数据窗体一般较为优良一点,可以再追加一个基类BaseEditForm,用来做新增编制窗体的基类,该基类继承自BaseForm类,工程代码如下所示。

正文思路是根源对Winform窗体的剖析切磋,发现里头不少优点,很多闪光的地点,结合自己在Winform方面的积攒及商讨,对当天嘎拉的代码举行进一步的提炼,于是撰写了本文,本文首要介绍继承窗体的包装使用。 

 882828九五至尊手机版 2

大家先来分析下吉日嘎拉其对窗体的规划代码,一步步开展深切的研商。

 其中运行例子的效果如下所示:

先是其也是规划一个窗体基类BaseForm,
其余窗体继承自BaseForm基类,那也是一种常用的规划。

 882828九五至尊手机版 3

 882828九五至尊手机版 4代码

 882828九五至尊手机版 5

        /// <summary>
        /// 加载窗体
        /// </summary>
        private void FormOnLoad()
        {
            // 多语言国际化加载
            this.Localization(this);
            // 获得权限
            this.GetPermission();
            // 设置要点
            this.ActiveControl = this.txtCode;
            this.txtCode.Focus();
        }
        #endregion

 

        private void FrmItemDetailsAdd_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                // 设置鼠标繁忙景色
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    this.FormOnLoad();
                }
                catch (Exception ex)
                {
                    this.ProcessException(ex);
                }
                finally
                {
                    // 设置鼠标默许状态
                    this.Cursor = Cursors.Default;
                }
            }
        }

 编辑以及新增我们构成在一个窗体中,先看看该基类的统筹视图,我们在里头添加了3个按钮(常用的充分、保存、关闭按钮)。

        #region private void SaveEntity(bool close)
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name=”close”>关闭窗体</param>
        private void SaveEntity(bool close)
        {
            // 检查输入的管事
            if (this.CheckInput())
            {
                // 设置鼠标繁忙景观
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        if (close)
                        {
                            // 关闭窗口
                            this.Close();
                        }else
                        {
                            this.ClearScreen();
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.ProcessException(ex);
                }
                finally
                {
                    // 设置鼠标默许状态
                    this.Cursor = Cursors.Default;
                }
            } 
        }
        #endregion

 882828九五至尊手机版 6

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.SaveEntity(false);
        }

窗体的代码我大类选择了可重载的虚函数,留给子类窗体举行落实不一致的拍卖操作,如窗体加载突显操作、显示数据到控件的操作、保存、新增等函数都是亟需达成的,而调用逻辑以及一些通用的处理,则在基类BaseEditForm中贯彻,子类不用重新那几个代码,按钮控件(添加、保存、关闭)的事件处理也曾经进展了逻辑封装,如下所示。 

        private void btnSave_Click(object sender, EventArgs e)
        {
            this.SaveEntity(true);
        }

 882828九五至尊手机版 7代码

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }  

882828九五至尊手机版 8

 

        public override void FormOnLoad()
        {
            base.FormOnLoad();
            if (!this.DesignMode)
            {
                if (!string.IsNullOrEmpty(ID))
                {
                    this.Text = “编辑 ” + this.Text;
                    this.btnAdd.Visible = false;//假如是编写,则屏蔽添加按钮
                }
                else
                {
                    this.Text = “新建 ” + this.Text;
                }
                
                DisplayData();
            }
        }

 

       /// <summary>
        /// 展现数据到控件上
        /// </summary>
        public virtual void DisplayData()
        {
        } 
        
        /// <summary>
        /// 检查输入的卓有成效
        /// </summary>
        /// <returns>有效</returns>
        public virtual bool CheckInput()
        {
            return true;
        }
               
        /// <summary>
        /// 清除屏幕
        /// </summary>
        public virtual void ClearScreen()
        {
            this.ID = “”;////要求安装为空,表示新增
            ClearControlValue(this);
            this.FormOnLoad();
        }
       
        /// <summary>
        /// 保存数据(新增和编辑的保留)
        /// </summary>
        public virtual bool SaveEntity()
        {
            bool result = false;
            if(!string.IsNullOrEmpty(ID))
            {
                //编辑的保留
                result = SaveUpdated();
            }
            else
            {
                //新增的保留
                result = SaveAddNew();
            }

从里面的卷入调用大家看看,吉日嘎拉已经把许多转移的代码抽象出来,也真是一个较好的布署,别的对窗体的输入回车转Tab也做了一个通用的转移,如下所示:

            return result;
        }

 882828九五至尊手机版 9代码

        /// <summary>
        /// 更新已有的数据
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveUpdated()
        {
            return true;
        }

        private void FrmItemDetailsAdd_KeyDown(object sender, KeyEventArgs e)
        {
            string keyCode = e.KeyCode.ToString();
            switch (keyCode)
            {
                case “Enter”:
                case “Return”:
                    if (this.ActiveControl is TextBox)
                    {
                        if (!((TextBox)this.ActiveControl).Multiline)
                        {
                            SendKeys.Send(“{TAB}”);
                        }
                    }
                    break;
            }
            switch (e.KeyValue)
            {
                case 116:
                    // 点击了F5按钮
                    this.FormOnLoad();
                    break;
            }
        }

        /// <summary>
        /// 保存新增的数量
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveAddNew()
        {
            return true;
        }

 

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name=”close”>关闭窗体</param>
        private void SaveEntity(bool close)
        {
            // 检查输入的有用
            if (this.CheckInput())
            {
                // 设置鼠标繁忙景观
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        MessageUtil.ShowTips(“保存成功”);
                        if (close)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            this.ClearScreen();
                        }                           
                    }
                }
                catch (Exception ex)
                {
                    this.ProcessException(ex);
                }
                finally
                {
                    // 设置鼠标默许状态
                    this.Cursor = Cursors.Default;
                }
            }
        }

 

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.SaveEntity(false);
        }

闪烁之处还有不少,本文暂时在此平息,介绍一下之中可以重构更好的地方。

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.SaveEntity(true);
        }

由于吉日嘎拉的窗体设计,对于一般常用到的编辑数据、新增多少窗体,分开了五个不等的窗体进行处理,而且由于BaseForm窗体没有对通用的函数进行尤其的虚幻,因而,编辑及新增窗体多了诸多双重累赘的代码,其实可以把增产、编辑合并一个窗体,然后根据新增、编辑二种差其他尺度举行处理即可。

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

是因为BaseForm一般需求在多数的窗体中,而新增编制数据窗体一般比较特殊一点,可以再充实一个基类BaseEditForm,用来做新增编制窗体的基类,该基类继承自BaseForm类,工程代码如下所示。

 

 882828九五至尊手机版 10

882828九五至尊手机版 11

 其中运行例子的成效如下所示:

 

 882828九五至尊手机版 12

里面值得介绍的是,窗体的控件数据清空在基类窗体中经过遍历完结了通用的数据清空操作,该片段代码引用了“潇湘隐者的博客(http://www.cnblogs.com/kerrycode/archive/2010/02/05/1664267.html),对此感谢。

 882828九五至尊手机版 13

别的,基类窗体还达成了Tab键的转换,那几个思路来源于即日嘎拉的代码,但由于是基类完成,有些不一致,他的位于每个具体的子类中,因而通用性有些限制。

 

大家重载了ProcessCmdKey(ref Message msg, Keys keyData)
函数后,就足以兑现统一的回车键转换了。

 编辑以及新增大家构成在一个窗体中,先看看该基类的统筹视图,我们在里边添加了3个按钮(常用的增进、保存、关闭按钮)。

882828九五至尊手机版 14

 882828九五至尊手机版 15

882828九五至尊手机版 16代码

窗体的代码我大类采纳了可重载的虚函数,留给子类窗体进行落到实处不一样的处理操作,如窗体加载展现操作、突显数据到控件的操作、保存、新增等函数都是须求贯彻的,而调用逻辑以及部分通用的拍卖,则在基类BaseEditForm中已毕,子类不用重新这一个代码,按钮控件(添加、保存、关闭)的事件处理也一度拓展了逻辑封装,如下所示。 

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {       
            if (keyData == Keys.F5)
            {
                this.FormOnLoad();
            }

 882828九五至尊手机版 17代码

            if ((!(ActiveControl is Button)) &&
                (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Enter))
            {
                if (keyData == Keys.Enter)
                {
                    System.Windows.Forms.SendKeys.Send(“{TAB}”);
                    return true;
                }
                if (keyData == Keys.Down)
                {
                    System.Windows.Forms.SendKeys.Send(“{TAB}”);
                }
                else
                {
                    SendKeys.Send(“+{Tab}”);
                }

        public override void FormOnLoad()
        {
            base.FormOnLoad();
            if (!this.DesignMode)
            {
                if (!string.IsNullOrEmpty(ID))
                {
                    this.Text = “编辑 ” + this.Text;
                    this.btnAdd.Visible = false;//要是是编写,则屏蔽添加按钮
                }
                else
                {
                    this.Text = “新建 ” + this.Text;
                }
                
                DisplayData();
            }
        }

                return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

       /// <summary>
        /// 展现数据到控件上
        /// </summary>
        public virtual void DisplayData()
        {
        } 
        
        /// <summary>
        /// 检查输入的得力
        /// </summary>
        /// <returns>有效</returns>
        public virtual bool CheckInput()
        {
            return true;
        }
               
        /// <summary>
        /// 清除屏幕
        /// </summary>
        public virtual void ClearScreen()
        {
            this.ID = “”;////必要安装为空,表示新增
            ClearControlValue(this);
            this.FormOnLoad();
        }
       
        /// <summary>
        /// 保存数据(新增和编辑的保留)
        /// </summary>
        public virtual bool SaveEntity()
        {
            bool result = false;
            if(!string.IsNullOrEmpty(ID))
            {
                //编辑的保留
                result = SaveUpdated();
            }
            else
            {
                //新增的保留
                result = SaveAddNew();
            }

882828九五至尊手机版 18

            return result;
        }

 

        /// <summary>
        /// 更新已部分数据
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveUpdated()
        {
            return true;
        }

 

        /// <summary>
        /// 保存新增的数量
        /// </summary>
        /// <returns></returns>
        public virtual bool SaveAddNew()
        {
            return true;
        }

 最终,大家看看现实的子类窗体,看新增编制界面须求贯彻的代码,如下所示,其中多数是原子级其余操作,逻辑操作已经在基类中贯彻了哦:

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name=”close”>关闭窗体</param>
        private void SaveEntity(bool close)
        {
            // 检查输入的灵光
            if (this.CheckInput())
            {
                // 设置鼠标繁忙景色
                this.Cursor = Cursors.WaitCursor;
                try
                {
                    if (this.SaveEntity())
                    {
                        MessageUtil.ShowTips(“保存成功”);
                        if (close)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            this.ClearScreen();
                        }                           
                    }
                }
                catch (Exception ex)
                {
                    this.ProcessException(ex);
                }
                finally
                {
                    // 设置鼠标默许状态
                    this.Cursor = Cursors.Default;
                }
            }
        }

 882828九五至尊手机版 19代码

        private void btnAdd_Click(object sender, EventArgs e)
        {
            this.SaveEntity(false);
        }

882828九五至尊手机版 20

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.SaveEntity(true);
        }

    public partial class FrmEditCustomer : BaseUI.BaseEditForm
    {
        public FrmEditCustomer()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        /// <summary>
        /// 已毕控件输入检查的函数
        /// </summary>
        /// <returns></returns>
        public override bool CheckInput()
        {
            bool result = true;

 

            #region 输入验证
            if (this.txtName.Text.Length == 0)
            {
                MessageUtil.ShowTips(“宾客名称不能够为空”);
                this.txtName.Focus();
                result = false;
            }
            else if (this.txtIDNumber.Text.Length == 0)
            {
                MessageUtil.ShowTips(“证件号码无法为空”);
                this.txtIDNumber.Focus();
                result = false;
            }

 

            #endregion

其中值得介绍的是,窗体的控件数据清空在基类窗体中通过遍历完毕了通用的数码清空操作,该有的代码引用了“潇湘隐者的博客(http://www.cnblogs.com/kerrycode/archive/2010/02/05/1664267.html),对此感谢。

            return result;
        }

其余,基类窗体还已毕了Tab键的变换,那一个思路来源于即日嘎拉的代码,但出于是基类完毕,有些不一致,他的放在每个具体的子类中,由此通用性有些限制。

882828九五至尊手机版,        /// <summary>
        /// 编辑或者封存处境下取值函数
        /// </summary>
        /// <param name=”info”></param>
        private void SetInfo(CustomerInfo info)
        {
            info.Address = txtAddress.Text;
            info.CompanyName = txtCompany.Text;
            info.IDCarType = cmbIDCarType.Text;
            info.Name = txtName.Text;
            info.Note = txtNote.Text;
            info.IDNumber = txtIDNumber.Text;
            info.Telephone = txtTelephone.Text;
            info.Source = cmbSource.Text;
            info.CustomerType = cmbType.Text;
            info.Sex = cmbSex.Text;
        }

大家重载了ProcessCmdKey(ref Message msg, Keys keyData)
函数后,就可以兑现统一的回车键转换了。

        /// <summary>
        /// 数据字典加载
        /// </summary>
        private void InitDictItem()
        {
            this.cmbSource.Items.Clear();
            this.cmbSource.Items.AddRange(DictItemUtil.GetCustomerSource());

882828九五至尊手机版 21882828九五至尊手机版 22代码

            this.cmbType.Items.Clear();
            this.cmbType.Items.AddRange(DictItemUtil.GetCustomerType());

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {       
            if (keyData == Keys.F5)
            {
                this.FormOnLoad();
            }

            this.cmbIDCarType.Items.Clear();
            this.cmbIDCarType.Items.AddRange(DictItemUtil.GetIDCarType());
        }

            if ((!(ActiveControl is Button)) &&
                (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Enter))
            {
                if (keyData == Keys.Enter)
                {
                    System.Windows.Forms.SendKeys.Send(“{TAB}”);
                    return true;
                }
                if (keyData == Keys.Down)
                {
                    System.Windows.Forms.SendKeys.Send(“{TAB}”);
                }
                else
                {
                    SendKeys.Send(“+{Tab}”);
                }

        /// <summary>
        /// 数据浮现的函数
        /// </summary>
        public override void DisplayData()
        {
            //数据字典加载(公用)
            InitDictItem();

                return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }

            if (!string.IsNullOrEmpty(ID))
            {
                //编辑状态下的数据突显
                CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(ID);
                if (info != null)
                {
                    #region 突显客户音信
                    txtAddress.Text = info.Address;
                    txtCompany.Text = info.CompanyName;
                    txtName.Text = info.Name;
                    txtNote.Text = info.Note;
                    txtIDNumber.Text = info.IDNumber;
                    txtTelephone.Text = info.Telephone;
                    cmbSource.Text = info.Source;
                    cmbType.Text = info.CustomerType;
                    cmbSex.Text = info.Sex;
                    cmbIDCarType.Text = info.IDCarType;
                    lblCreateDate.Text = info.RegistrationDate.ToString();

 

                    #endregion
                }
            }
            else
            {
                //新增状态的多少展现
                lblCreateDate.Text = DateTime.Now.ToString();
            }
        }

 

        /// <summary>
        /// 新增状态下的数据保存
        /// </summary>
        /// <returns></returns>
        public override bool SaveAddNew()
        {
            CustomerInfo info = new CustomerInfo();
            SetInfo(info);
            info.RegistrationDate = DateTime.Now;
            bool succeed = BLLFactory<Customer>.Instance.Insert(info);
            return succeed;
        }

 最终,大家看看实际的子类窗体,看新增编制界面要求贯彻的代码,如下所示,其中一大半是原子级其他操作,逻辑操作已经在基类中落到实处了哦:

        /// <summary>
        /// 编辑状态下的数目保存
        /// </summary>
        /// <returns></returns>
        public override bool SaveUpdated()
        {
            CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(ID);
            if (info != null)
            {
                SetInfo(info);
                bool succeed = BLLFactory<Customer>.Instance.Update(info, info.ID.ToString());
                return succeed;
            }
            return false;
        }
    }

 882828九五至尊手机版 23代码

882828九五至尊手机版 24

    public partial class FrmEditCustomer : BaseUI.BaseEditForm
    {
        public FrmEditCustomer()
        {
            InitializeComponent();
        }

 

        /// <summary>
        /// 达成控件输入检查的函数
        /// </summary>
        /// <returns></returns>
        public override bool CheckInput()
        {
            bool result = true;

            #region 输入验证
            if (this.txtName.Text.Length == 0)
            {
                MessageUtil.ShowTips(“宾客名称不可能为空”);
                this.txtName.Focus();
                result = false;
            }
            else if (this.txtIDNumber.Text.Length == 0)
            {
                MessageUtil.ShowTips(“证件号码不可能为空”);
                this.txtIDNumber.Focus();
                result = false;
            }

            #endregion

            return result;
        }

        /// <summary>
        /// 编辑或者封存意况下取值函数
        /// </summary>
        /// <param name=”info”></param>
        private void SetInfo(CustomerInfo info)
        {
            info.Address = txtAddress.Text;
            info.CompanyName = txtCompany.Text;
            info.IDCarType = cmbIDCarType.Text;
            info.Name = txtName.Text;
            info.Note = txtNote.Text;
            info.IDNumber = txtIDNumber.Text;
            info.Telephone = txtTelephone.Text;
            info.Source = cmbSource.Text;
            info.CustomerType = cmbType.Text;
            info.Sex = cmbSex.Text;
        }

        /// <summary>
        /// 数据字典加载
        /// </summary>
        private void InitDictItem()
        {
            this.cmbSource.Items.Clear();
            this.cmbSource.Items.AddRange(DictItemUtil.GetCustomerSource());

            this.cmbType.Items.Clear();
            this.cmbType.Items.AddRange(DictItemUtil.GetCustomerType());

            this.cmbIDCarType.Items.Clear();
            this.cmbIDCarType.Items.AddRange(DictItemUtil.GetIDCarType());
        }

        /// <summary>
        /// 数据显示的函数
        /// </summary>
        public override void DisplayData()
        {
            //数据字典加载(公用)
            InitDictItem();

            if (!string.IsNullOrEmpty(ID))
            {
                //编辑状态下的数额彰显
                CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(ID);
                if (info != null)
                {
                    #region 突显客户音信
                    txtAddress.Text = info.Address;
                    txtCompany.Text = info.CompanyName;
                    txtName.Text = info.Name;
                    txtNote.Text = info.Note;
                    txtIDNumber.Text = info.IDNumber;
                    txtTelephone.Text = info.Telephone;
                    cmbSource.Text = info.Source;
                    cmbType.Text = info.CustomerType;
                    cmbSex.Text = info.Sex;
                    cmbIDCarType.Text = info.IDCarType;
                    lblCreateDate.Text = info.RegistrationDate.ToString();

                    #endregion
                }
            }
            else
            {
                //新增状态的数量展现
                lblCreateDate.Text = DateTime.Now.ToString();
            }
        }

        /// <summary>
        /// 新增状态下的数码保存
        /// </summary>
        /// <returns></returns>
        public override bool SaveAddNew()
        {
            CustomerInfo info = new CustomerInfo();
            SetInfo(info);
            info.RegistrationDate = DateTime.Now;
            bool succeed = BLLFactory<Customer>.Instance.Insert(info);
            return succeed;
        }

        /// <summary>
        /// 编辑状态下的多上大夫存
        /// </summary>
        /// <returns></returns>
        public override bool SaveUpdated()
        {
            CustomerInfo info = BLLFactory<Customer>.Instance.FindByID(ID);
            if (info != null)
            {
                SetInfo(info);
                bool succeed = BLLFactory<Customer>.Instance.Update(info, info.ID.ToString());
                return succeed;
            }
            return false;
        }
    }

 

 

相关文章

Your Comments

近期评论

    功能


    网站地图xml地图