在Winform项目中,界面美观也是需要的,经常遇到界面元素不统一的情况,比如Button有大有小,比如都是保存,但每个页

面的快捷键还不一定相同,为在项目中统一元素,我将Button进行了封装,把常用的按钮统一到一个控件中:

1、首先定义一个Button类型枚举类

 1         public enum ButtonType
 2         {
 3             /// <summary>
 4             /// 保存
 5             /// </summary>
 6             Save = 0,
 7 
 8             /// <summary>
 9             /// 退出
10             /// </summary>
11             Quit = 1,
12 
13             /// <summary>
14             /// 确定
15             /// </summary>
16             Confirm = 2,
17 
18             /// <summary>
19             /// 取消
20             /// </summary>
21             Cancle = 3,
22 
23             /// <summary>
24             /// 新建
25             /// </summary>
26             New = 4,
27 
28             /// <summary>
29             /// 编辑
30             /// </summary>
31             Edit = 5,
32 
33             /// <summary>
34             /// 删除
35             /// </summary>
36             Delete = 6,
37            
38             /// <summary>
39             /// 查询
40             /// </summary>
41             Query = 7,
42 
43             /// <summary>
44             /// 应用
45             /// </summary>
46             Apply = 8,
47 
48             /// <summary>
49             /// 添加
50             /// </summary>
51             Add = 9,
52 
53             /// <summary>
54             /// 自定义
55             /// </summary>
56             Other = 99
57         }
View Code

2、建一个统一Button类,集成Button控件

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.ComponentModel;
 5 using System.Linq;
 6 using System.Text;
 7 using System.Threading.Tasks;
 8 using System.Windows.Forms;
 9 
10 namespace UcTool
11 {
12     public class UcButton : Button
13     {
14 
15         public UcButton()
16         {
17             //初始化按钮类型枚举
18             InitButtonDictionary();
19             //初始化按钮样式
20             InitButtonStyle();            
21         }
22 
23         private void InitButtonStyle()
24         {
25             base.Size = new System.Drawing.Size(93, 30);
26         }
27 
28         #region 按钮属性
29         private ButtonType _B_Type;
30         /// <summary>
31         /// 按钮类型
32         /// </summary>       
33         [Description("按钮类型")]
34         public ButtonType B_Type
35         {
36             get
37             {
38                 return _B_Type;
39             }
40             set
41             {
42                 _B_Type = value;
43                 Text = ButtonDictionary[value];
44             }
45         }
46 
47         [Description("按钮文本")]        
48         public new string Text
49         {
50             get { return base.Text; }
51             set { base.Text = value; }
52         }
53         
54         #endregion
55 
56         #region 一次性载入按钮字典
57         private static Dictionary<ButtonType, string> ButtonDictionary;
58         private void InitButtonDictionary()
59         {
60             if (ButtonDictionary == null || ButtonDictionary.Count == 0)
61             {
62                 ButtonDictionary = new Dictionary<ButtonType, string>();
63                 ButtonDictionary.Add(ButtonType.Save, "保存(&S)");
64                 ButtonDictionary.Add(ButtonType.Quit, "退出(&U)");
65                 ButtonDictionary.Add(ButtonType.Confirm, "确定(&O)");
66                 ButtonDictionary.Add(ButtonType.Cancle, "取消(&C)");
67                 ButtonDictionary.Add(ButtonType.New, "新建(&N)");
68                 ButtonDictionary.Add(ButtonType.Edit, "编辑(&E)");
69                 ButtonDictionary.Add(ButtonType.Delete, "删除(&D)");
70                 ButtonDictionary.Add(ButtonType.Query, "查询(&Q)");
71                 ButtonDictionary.Add(ButtonType.Apply, "应用(&A)");
72                 ButtonDictionary.Add(ButtonType.Add, "添加(&M)");
73                 ButtonDictionary.Add(ButtonType.Other, "自定义");
74             }
75         }
76         #endregion
77     }
78 }
View Code

在使用的过程中遇到常用的按钮只需要修改该Button的【B_Type】属性即可,后期也可统一对按钮的样式进行调整;

posted on 2013-05-16 18:41  蓝色狙击手  阅读(1416)  评论(4编辑  收藏  举报