C#用户控件开发

简介:   控件名称: chkSelect label1 BeginDatePicker EndDatePicker   控件代码如下(直接从UserControl继承)  1 using System; 2 using System.

 

 

控件名称:

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 }

 

 

 

相关文章
|
1月前
|
C# 数据库 开发者
44.c#:combobox控件
44.c#:combobox控件
16 1
|
1月前
|
C# 数据库
40.c#:TreeView 控件
40.c#:TreeView 控件
14 1
|
8天前
|
开发框架 前端开发 JavaScript
采用C#.Net +JavaScript 开发的云LIS系统源码 二级医院应用案例有演示
技术架构:Asp.NET CORE 3.1 MVC + SQLserver + Redis等 开发语言:C# 6.0、JavaScript 前端框架:JQuery、EasyUI、Bootstrap 后端框架:MVC、SQLSugar等 数 据 库:SQLserver 2012
|
1月前
|
C# Windows
49.c#:StatusStrip 控件
49.c#:StatusStrip 控件
23 1
49.c#:StatusStrip 控件
|
1月前
|
C# 开发者 Windows
48.c#:toolstrip控件
48.c#:toolstrip控件
15 1
|
1月前
|
C# Windows
47.c#:menustrip控件
47.c#:menustrip控件
13 1
|
1月前
|
存储 缓存 C#
46.c#:datagridview控件
46.c#:datagridview控件
18 1
|
1月前
|
C#
45.c#:listview控件
45.c#:listview控件
10 1
|
1月前
|
C# 数据库 虚拟化
43.c#:listbox控件
43.c#:listbox控件
15 1
|
1月前
|
数据处理 C# UED
42.c#:progressbar控件
42.c#:progressbar控件
15 1