玩转控件:封装Dev的LabelControl和TextEdit

03/25 17:58
阅读数 14

  俗话说的好:"工欲善其事必先利其器",作为软件攻城狮也是同样道理,攻城狮开发的软件目的是简化客户的操作,让客户动动手指就可以完成很多事情,减少人力成本。这也是系统/软件存在的目的。那对于攻城狮来说怎么简化自己的操作?让自己也动动手指就可以减少很多重复的工作呢?如果你对此也有同样的疑问或兴趣,那就跟作者一起看下去吧!

  说到利器,自然而然的会想到宇宙第一IDE——Visual Studio 随着版本的迭代,大大的加快了攻城狮的编码效率。那对于我们自己开发的软件,如何减少重复操作,加快软件进度呢?这就是作者今日的主题——封装控件。

        作者几年前有幸作为公司代表,到金蝶总部参观,并学习当年金蝶最新科技——K3 Cloud,说实话,作者当年年轻气盛,和普遍技术开发一样,心高气傲,对除自己研发以外的软件不屑一顾,觉得自己才是最流P的!现在好了,善恶终有报,轮到自己爬着学习K3 Cloud的控件封装来造轮子了...

   想当年,金戈铁马,气吞万里如虎!....现如今,想起来都是泪...

        扯远了,还是回到本篇的主题造轮子...哦,不!玩转控件!封装自己的插件吧!

        今天作者主要介绍的是,封装Dev的LabelControl和TextEdit。原因很简单,说的好听就是简化自己重复操作,封装前,拖控件大法,先拖一个LabelControl ,完善它的属性。在拖一个TextEdit,完善它的属性,必要时还要完善它的控件。你以为现在就完了吗?不对,还要拖动一下,看看上下左右的对齐效果....emmmmm~

   Talk is Cheap,Show me the Code!

   首先我们先新建一个用户控件,继承UserControl,为了减少冗余代码,作者把命名为BaseControl,后续所有用户控件全部继承此类。

public partial class KzxBaseControl : System.Windows.Forms.UserControl,IControl

   其中IControl接口主要用来声明,控件的一些属性和事件,部分代码如下(代码篇幅较长,有需要公众号call我,源码免费赠送):

/// <summary>
    /// 控件事件委托
    /// </summary>
    /// <param name="sender">事件发起者</param>
    /// <param name="e">事件参数</param>
    public delegate void KzxControlOperateEventHandler(object sender, ControlEventArgs e);

    /// <summary>
    /// 获取多语言文本事件委托
    /// </summary>
    /// <param name="sender">事件发起者</param>
    /// <param name="messageCode">语言标识</param>
    /// <param name="text">多语言的文本</param>
    public delegate void KzxGetLanguageEventHandler(object sender, string messageCode, ref string text);
    public interface IControl
    {
        /// <summary>
        /// 有Load方法
        /// </summary>
        bool HasLoad { get; }

        /// <summary>
        /// 被引用后允许修改
        /// true允许,false不允许
        /// </summary>
        bool AllowEdit { get; set; }

        /// <summary>
        /// 多语言环境下显示文本的对应标识
        /// </summary>
        string MessageCode { get; set; }

        /// <summary>
        /// 设计时的显示,方便设计员工识别
        /// </summary>
        string DesigeCaption { get; set; }

        /// <summary>
        /// 控件的唯一标识
        /// </summary>
        string Key { get; set; }

        /// <summary>
        /// True控件可用,False控件不可用
        /// </summary>
        Boolean Enabled { get; set; }

        /// <summary>
        /// True控件可见,False控件不可见
        /// </summary>
        Boolean Visible { get; set; }

        /// <summary>
        /// Tag标志,用于存储任何数据
        /// </summary>
        object Tag { get; set; }

        /// <summary>
        /// 设计时的可用性
        /// </summary>
        Boolean DesigeEnabled { get; set; }
        ...
        ...

   用户控件父类,主要集中所有用户控件的通用属性、方法和事件,部分代码如下:

private bool _AllowEdit = true;
  /// <summary>
  /// 被引用后允许修改
  /// true允许,false不允许
  /// </summary>
  [Category("验证"), Description("AllowEdit,被引用后允许修改,true允许,false不允许"), Browsable(true)]
  [McDisplayName("AllowEdit")]
  public virtual bool AllowEdit
  {
      get
      {
          return this._AllowEdit;
      }
      set
      {
          this._AllowEdit = value;
      }
  }
  
  private string _MessageCode = "0";
  /// <summary>
  /// 多语言环境下显示文本的对应标识
  /// </summary>
  [Category("多语言"), Description("MessageCode,多语言环境下显示文本的对应标识"), Browsable(true)]
  [McDisplayName("MessageCode")]
  public virtual string MessageCode
  {
      get
      {
          return this._MessageCode;
      }
      set
      {
          this._MessageCode = value;
      }
  }
  
  private string _DesigeCaption = "显示标题";
  /// <summary>
  /// 没有多语言的情况下的默认显示标题
  /// </summary>
  [Category("多语言"), Description("DesigeCaption,没有多语言的情况下的默认显示标题"), Browsable(true)]
  [McDisplayName("DesigeCaption")]
  public virtual string DesigeCaption
  {
      get
      {
          return this._DesigeCaption;
      }
      set
      {
          this._DesigeCaption = value;
      }
  }
  
  private string _Key = string.Empty;
  /// <summary>
  /// 控件的唯一标识
  /// </summary>
  [Category("数据"), Description("Key,控件的唯一标识"), Browsable(true)]
  [McDisplayName("Key")]
  public virtual string Key
  {
      get
      {
          if (string.IsNullOrWhiteSpace(this._Key) == true)
          {
              if (string.IsNullOrWhiteSpace(this.Table) == false && string.IsNullOrWhiteSpace(this.Field) == false)
              {
                  this._Key = this.Table + "." + this.Field;
              }
              else if (string.IsNullOrWhiteSpace(this.Table) == false)
              {
                  this._Key = this.Table;
              }
          } 
          if (string.IsNullOrEmpty(this._Key)) return this.Name; 
  
          return this._Key;
      }
      set
      {
          this._Key = value;
      }
  }
  
  private bool _DesigeEnabled = true;
  /// <summary>
  /// 设计时的可用性
  /// </summary>
  [Category("特性"), Description("DesigeEnabled,设计时的可用性"), Browsable(true)]
  [McDisplayName("DesigeEnabled")]
  public virtual bool DesigeEnabled
  {
      get
      {
          return this._DesigeEnabled;
      }
      set
      {
          this._DesigeEnabled = value;
          //this.Enabled = value;
      }
  }
  
  private bool _DesigeVisible = true;
  /// <summary>
  /// 设计时可见性
  /// </summary>
  [Category("特性"), Description("DesigeVisible,设计时可见性"), Browsable(true)]
  [McDisplayName("DesigeVisible")]
  public virtual bool DesigeVisible
  {
      get
      {
          return this._DesigeVisible;
      }
      set
      {
          this._DesigeVisible = value;
          //this.Visible = value;
          if (this.DesignMode == true)
          {
              if (value == false)
              {
                  this.BorderStyle = BorderStyle.Fixed3D;
              }
              else
              {
                  this.BorderStyle = BorderStyle.None;
              }
          }
          else
          {
              //this.Visible = value;
          }
      }
  }
/// <summary>
/// 触发控件事件
/// </summary>
/// <param name="sender">事件发起者</param>
/// <param name="eventName">事件名称</param>
/// <param name="e">事件参数</param>
protected virtual void RaiseEvent(object sender, string eventName, object e)
{
    ControlEventArgs args = new ControlEventArgs();
    args.CurrentControl = sender;
    args.EventId = eventName;
    args.SystemEventArgs = e;
    args.FieldName = this.Field;
    args.TableName = this.Table;
    args.Key = this.Key;
    if (this.KzxControlOperate != null)
    {
        this.KzxControlOperate(this, args);
        e = args.SystemEventArgs;
    }
}


private static MethodInfo _methodInfo = null;

/// <summary>
/// 获取多语言文本
/// </summary>
/// <param name="messageCode">语言文本标识</param>
/// <param name="defaultMessage">默认的文本</param>
/// <returns>取到的文本</returns>
protected virtual string GetLanguage(string messageCode, string defaultMessage)
{
    string text = string.Empty;

    try
    {
        text = defaultMessage;
        string filepath = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "KzxCommon.dll");
        Assembly assembly = null;
        object obj = null;

        Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
        for (int i = 0; i < assemblies.Length; i++)
        {
            if (assemblies[i].GetName().Name.Equals("KzxCommon", StringComparison.OrdinalIgnoreCase) == true)
            {
                assembly = assemblies[i];
                break;
            }
        }
        if (assembly == null)
        {
            assembly = Assembly.LoadFrom(filepath);
        }
        obj = assembly.CreateInstance("KzxCommon.sysClass");
        text = defaultMessage;
        if (_methodInfo == null)
        {
            if (obj != null)
            {
                _methodInfo = obj.GetType().GetMethod("ssLoadMsg", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                if (_methodInfo != null)
                {
                    text = _methodInfo.Invoke(obj, new object[] { messageCode }).ToString();
                }
            }
        }
        else
        {
            text = _methodInfo.Invoke(obj, new object[] { messageCode }).ToString();
        }
    }
    catch (Exception ex)
    {

    }
    return string.IsNullOrWhiteSpace(text) == true ? defaultMessage : text;
}

protected virtual void OnKzxBaseControlLoad()
{
    object obj = null;
    PropertyInfo pi = null;
    for (int i = 0; i < this.Controls.Count; i++)
    {
        if (this.Controls[i].Name.Equals("ValueControl", StringComparison.OrdinalIgnoreCase) == true)
        {
            pi = this.Controls[i].GetType().GetProperty("ErrorIconAlignment");
            if (pi != null)
            {
                pi.SetValue(this.Controls[i], ErrorIconAlignment.TopRight, null);
            }
        }
    }
}

protected override void OnControlAdded(System.Windows.Forms.ControlEventArgs e)
{
    base.OnControlAdded(e);
    OnKzxBaseControlLoad();
    SetAppearance();
}

  介绍完基类,我们新建在新建要给用户控件来继承它,并实现业务需求:

/// <summary>
    /// 文本框验证
    /// </summary>
    [ToolboxBitmapAttribute(typeof(Bitmap), "文本框")]
    public partial class KzxTextBox : KzxBaseControl

   布局方面,应自己要求,气运丹田,使出拖控件大法!

   此处无需做任何属性、事件设置。只需把我们日常常用到的属性、事件,用特性标记起来即可。部分代码如下:

private DevExpress.XtraEditors.Controls.BorderStyles _BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Default;
/// <summary>
/// 边框显示格式
/// </summary>
[Category("数据格式"), Description("KzxBorderStyle,边框显示格式"), Browsable(true)]
[McDisplayName("KzxBorderStyle")]
public override DevExpress.XtraEditors.Controls.BorderStyles KzxBorderStyle
{
    get
    {
        return this.ValueControl.Properties.BorderStyle;
    }
    set
    {
        this._BorderStyle = value;
        this.ValueControl.Properties.BorderStyle = value;
    }
}

/// <summary>
/// 没有多语言的情况下的默认显示标题
/// </summary>
[Category("多语言"), Description("DesigeCaption,没有多语言的情况下的默认显示标题"), Browsable(true)]
[McDisplayName("DesigeCaption")]
public override string DesigeCaption
{
    get
    {
        return this.CaptionControl.Text.Trim();
    }
    set
    {
        this.CaptionControl.Text = value;
    }
}

private bool _IsNull = true;
/// <summary>
/// 可空性
/// </summary>
[Category("验证"), Description("IsNull,可空性"), Browsable(true)]
[McDisplayName("IsNull")]
public override bool IsNull
{
    get
    {
        SetBackColor();
        return this._IsNull;
    }
    set
    {
        this._IsNull = value;
        SetBackColor();
    }
}

/// <summary>
/// 只读性
/// </summary>
[Category("验证"), Description("ReadOnly,只读性"), Browsable(true)]
[McDisplayName("ReadOnly")]
public override bool ReadOnly
{
    get
    {
        SetBackColor();
        return this.ValueControl.Properties.ReadOnly;
    }
    set
    {
        this.ValueControl.Properties.ReadOnly = value;
        SetBackColor();
         if (value == false)
        {
            this.ValueControl.BackColor = Color.White;
        }
    }
}

private int maxLength = 0;
/// <summary>
/// 可录入的最大长度
/// </summary>
[Category("验证"), Description("MaxLength,可录入的最大长度"), Browsable(true)]
[McDisplayName("MaxLength")]
public override int MaxLength
{
    get
    {
        return maxLength;
    }
    set
    {
        maxLength = value;
    }
}

private Int32 _CaptionLabelWidth = 75;
/// <summary>
/// 显示标题宽度
/// </summary>
[Category("外观"), Description("CaptionLabelWidth,显示标题宽度"), Browsable(true)]
[McDisplayName("CaptionLabelWidth")]
public Int32 CaptionLabelWidth
{
    get
    {
        return this.CaptionControl.Width;
    }
    set
    {
        this._CaptionLabelWidth = value;
        this.CaptionControl.Width = value;
    }
}

private string toolTipMaxLengthText = string.Empty;
/// <summary>
/// 数据长度不能超过数据库长度提示文本  
/// </summary>
public override string ToolTipMaxLengthText
{
    get { return toolTipMaxLengthText; }
    set { toolTipMaxLengthText = value; }
}

/// <summary>
/// 提示信息
/// </summary>
[Category("汽泡提示"), Description("ToolTipText,提示信息"), Browsable(true)]
[McDisplayName("ToolTipText")]
public override string ToolTipText
{
    get
    {
        return (ValueControl == null) == true ? string.Empty : ValueControl.ToolTip;
    }
    set
    {
        if (ValueControl != null)
        {
            ValueControl.ToolTip = value;
        }
        if (CaptionControl != null)
        {
            CaptionControl.ToolTip = value;
        }
    }
}



private string _ToolTipMessageCode = string.Empty;
/// <summary>
/// 提示多语言标识
/// </summary>
[Category("汽泡提示"), Description("ToolTipMessageCode,提示信息多语言标识"), Browsable(true)]
[McDisplayName("ToolTipMessageCode")]
public override string ToolTipMessageCode
{
    get
    {
        return this._ToolTipMessageCode;
    }
    set
    {
        this._ToolTipMessageCode = value;
    }
}

    一起看看用户控件效果以及封装的属性事件:

    F5看看运行效果:

    Done! 一个控件,减少一半操作量! 在此,本控件当作作者抛砖引玉,看官们可以根据自己实际情况进行数据封装。有效的封装,避免画蛇添足哦~

        最后,由于后续所有重写/重绘控件都在同一个项目使用,而且Dev系统引用文件较多,压缩后源码文件仍然很大,如果有需要源码的朋友,可以微信公众号联系博主,源码可以免费赠予~!有疑问的也可以CALL我一起探讨,最最后,如果觉得本篇博文对您或者身边朋友有帮助的,麻烦点个关注!赠人玫瑰,手留余香,您的支持就是我写作最大的动力,感谢您的关注,期待和您一起探讨!再会!

原文出处:https://www.cnblogs.com/axing/p/12567785.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部