• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#用户控件开发

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

 

控件名称:

chkSelect

label1

BeginDatePicker

EndDatePicker

 

控件代码如下(直接从UserControl继承) 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Drawing;
  5 using System.Data;
  6 using System.Linq;
  7 using System.Text;
  8 using System.Windows.Forms;
  9 
 10 namespace CustomControlSample
 11 {
 12     [ToolboxBitmap(typeof(System.Windows.Forms.DateTimePicker))]
 13     public partial class DateTimeBeginEnd : UserControl
 14     {
 15         public DateTimeBeginEnd()
 16         {
 17             InitializeComponent();
 18             this.FCheckboxVisible = true;
 19             this.BeginDatePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
 20             this.EndDatePicker.Format = System.Windows.Forms.DateTimePickerFormat.Custom;
 21             this.BeginDatePicker.CustomFormat = "yyyy-MM-dd HH:mm:ss";
 22             this.EndDatePicker.CustomFormat = "yyyy-MM-dd HH:mm:ss";
 23             this.BeginDatePicker.Left=nBeginDateLeft;
 24         }
 25 
 26         private int nBeginDateLeft=25;
 27 
 28         private DateTime FBeginDate;
 29         private DateTime FEndDate;
 30         private Boolean FCheckDateTime;
 31         private Boolean FCheckboxVisible;
 32         private Boolean FCheckboxChecked;
 33         private DateTimePickerFormat FDateTimeFormat;
 34         private string FDateTimeCustomFormat;
 35 
 36         public delegate void CustomFormatChangeEventHandle(object sender, EventArgs arg);
 37         public event CustomFormatChangeEventHandle FormatChanged;
 38 
 39         private void OnFormatChanged(EventArgs e)
 40         {
 41             if (this.FormatChanged!=null)
 42             {
 43                 this.FormatChanged(this,e);
 44             }
 45         }
 46 
 47         public DateTime BeginDate 
 48         {
 49             get
 50             {
 51                 this.FBeginDate = this.BeginDatePicker.Value;
 52                 return this.FBeginDate;
 53             }
 54             set
 55             {
 56                 this.FBeginDate = value;
 57                 this.BeginDatePicker.Value = this.FBeginDate;
 58             }
 59         }
 60 
 61         public DateTime EndDate
 62         {
 63             get
 64             {
 65                 this.FEndDate = this.EndDatePicker.Value;
 66                 return this.FEndDate;
 67             }
 68             set
 69             {
 70                 this.FEndDate = value;
 71                 this.EndDatePicker.Value =this.FEndDate;
 72             }
 73         }
 74 
 75         public Boolean CheckDateTime
 76         {
 77             get
 78             {
 79                 this.FCheckDateTime = this.chkSelect.Checked;
 80                 return this.FCheckDateTime;
 81             }
 82             set
 83             {
 84                 this.FCheckDateTime = value;
 85                 this.chkSelect.Checked = this.FCheckDateTime;
 86             }
 87         }
 88 
 89         public Boolean CheckboxVisible
 90         {
 91             get
 92             {
 93                 this.FCheckboxVisible = this.chkSelect.Visible;
 94                 return this.FCheckboxVisible;
 95             }
 96             set
 97             {
 98                 this.FCheckboxVisible = value;
 99                 this.chkSelect.Visible = this.FCheckboxVisible;
100             }
101         }
102 
103         public DateTimePickerFormat DateTimeFormat
104         {
105             get
106             {
107                 this.FDateTimeFormat = this.BeginDatePicker.Format;
108                 return this.FDateTimeFormat;
109             }
110             set
111             {
112                 if (FDateTimeFormat != value)
113                 {
114                     this.FDateTimeFormat = value;
115                     this.BeginDatePicker.Format = this.FDateTimeFormat;
116                     this.EndDatePicker.Format = this.FDateTimeFormat;
117                     OnFontChanged(null);
118                 }
119             }
120         }
121 
122         public string DateTimeCustomFormat
123         {
124             get
125             {
126                 this.FDateTimeCustomFormat = this.BeginDatePicker.CustomFormat;
127                 return this.FDateTimeCustomFormat;
128             }
129             set
130             {
131                 this.FDateTimeCustomFormat = value;
132                 this.BeginDatePicker.CustomFormat = this.FDateTimeCustomFormat;
133                 this.EndDatePicker.CustomFormat = this.FDateTimeCustomFormat;
134             }
135         }
136 
137         public Boolean CheckboxChecked
138         {
139             get
140             {
141                 this.FCheckboxChecked = this.chkSelect.Checked;
142                 return this.FCheckboxChecked;
143             }
144             set
145             {
146                 this.FCheckboxChecked = value;
147                 this.chkSelect.Checked = this.FCheckboxChecked;
148             }
149         }
150 
151         public event EventHandler BeginDatePickerChange;
152         public event EventHandler EndDatePickerChange;
153         public event EventHandler CheckBoxClick;
154 
155         protected void OnBeginDateChange(EventArgs arg)
156         {
157             if (BeginDatePickerChange != null)
158                 BeginDatePickerChange(this, arg);
159         }
160 
161         protected void OnEndDateChange(EventArgs arg)
162         {
163             if (EndDatePickerChange != null)
164                 EndDatePickerChange(this, arg);
165         }
166 
167         protected void OnCheckBoxChange(EventArgs arg)
168         {
169             if (CheckBoxClick != null)
170                 CheckBoxClick(this,arg);
171         }
172 
173         private void EndDatePicker_ValueChanged(object sender, EventArgs e)
174         {
175             OnEndDateChange(e);
176         }
177 
178         private void BeginDatePicker_ValueChanged(object sender, EventArgs e)
179         {
180             OnBeginDateChange(e);
181         }
182 
183         private void chkSelect_Click(object sender, EventArgs e)
184         {
185             OnCheckBoxChange(e);
186         }
187 
188         private void EndDatePicker_FormatChanged(object sender, EventArgs e)
189         {
190             OnFormatChanged(e);
191             TextChange();
192         }
193 
194         private void BeginDatePicker_FormatChanged(object sender, EventArgs e)
195         {
196             OnFormatChanged(e);
197             TextChange();
198         }
199 
200         private void EndDatePicker_FontChanged(object sender, EventArgs e)
201         {
202             OnFormatChanged(e);
203             TextChange();
204             //if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
205             //{
206             //    //returnFlag = true;  DesignMode设计时状态
207             //}
208             //else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToUpper().Equals("DEVENV"))
209             //{
210             //    //returnFlag = true;
211             //}  
212         }
213 
214         private void TextChange()
215         {
216             this.BeginDatePicker.Width = TextRenderer.MeasureText(this.BeginDatePicker.Value.ToString(this.BeginDatePicker.CustomFormat), this.BeginDatePicker.Font).Width+37;
217             this.EndDatePicker.Width = TextRenderer.MeasureText(this.EndDatePicker.Value.ToString(this.EndDatePicker.CustomFormat), this.EndDatePicker.Font).Width + 37;
218             this.label1.Left =this.BeginDatePicker.Left+this.BeginDatePicker.Width+2;
219             this.EndDatePicker.Left = this.label1.Left + this.label1.Width + 2;
220         }
221 
222         private void BeginDatePicker_FontChanged(object sender, EventArgs e)
223         {
224             OnFormatChanged(e);
225             TextChange();
226         }
227 
228         private void DateTimeBeginEnd_Resize(object sender, EventArgs e)
229         {
230             //int nLabelLeft = 0;
231             //int nEndDateLeft = 0;
232             //nLabelLeft = this.BeginDatePicker.Left + this.BeginDatePicker.Width + 2;
233             //this.label1.Left = nLabelLeft;
234             //nEndDateLeft = this.label1.Left + this.label1.Width + 2;
235             //this.EndDatePicker.Left = nEndDateLeft;
236             this.BeginDatePicker.Width=(this.Width-25-label1.Width-6)/2;
237             this.EndDatePicker.Width=this.BeginDatePicker.Width;
238             this.label1.Left=this.BeginDatePicker.Left + this.BeginDatePicker.Width + 2;
239             this.EndDatePicker.Left =this.label1.Left + this.label1.Width + 2;
240         }
241     }
242 }

 

 

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Objective-C——消息、Category和Protocol发布时间:2022-07-13
下一篇:
C++拷贝构造函数(深拷贝,浅拷贝)发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap