winform 数字输入控件初稿

直接贴上代码
  1 using System;
  2 using System.ComponentModel;
  3 using System.Globalization;
  4 using System.Runtime.InteropServices;
  5 using System.Windows.Forms;
  6 
  7 namespace SimpleControl
  8 {
  9     /*
 10      * 1.只能输入数字和小数点
 11      * 2.在最前面输入0无效
 12      * 3.只能输入一个小数点,输入小数点时检查是否已经存在小数点了,存在就把光标移到小数点的后面
 13      */
 14     //[ToolboxBitmap(typeof(NumericTextBox), "NumericTextBox.ico")]
 15     public class SimNumericTextBox : TextBox, ISupportInitialize
 16     {
 17 
 18         #region -- ISupportInitialize --
 19         public void BeginInit()
 20         {
 21 
 22         }
 23 
 24         public void EndInit()
 25         {
 26 
 27         }
 28         #endregion -- ISupportInitialize --
 29 
 30         #region -- 属性 --
 31         public event EventHandler ValueChanged;
 32 
 33         bool showUpDown = true;
 34         public bool ShowUpDown
 35         {
 36             get { return showUpDown; }
 37             set { showUpDown = value; }
 38         }
 39 
 40         public double increment = 1;
 41         public double Increment
 42         {
 43             get { return increment; }
 44             set { increment = value; }
 45         }
 46 
 47 
 48         public override string Text
 49         {
 50             get
 51             {
 52                 return base.Text;
 53             }
 54             set
 55             {
 56                 try
 57                 {
 58                     double d = 0;
 59                     double.TryParse(value, out d);
 60                     base.Text = d.ToString(displayFormat);
 61                     return;
 62                 }
 63                 catch
 64                 {
 65                 }
 66             }
 67         }
 68 
 69         public double Value
 70         {
 71             get
 72             {
 73                 double d = 0;
 74                 double.TryParse(this.Text, out d);
 75                 return d;
 76             }
 77             set
 78             {
 79                 try
 80                 {
 81                     base.Text = value.ToString(displayFormat);
 82                     return;
 83                 }
 84                 catch
 85                 {
 86                 }
 87             }
 88         }
 89 
 90         private string displayFormat = "f2";
 91         [DefaultValue("f2")]
 92         public string DisplayFormat
 93         {
 94             get
 95             {
 96                 return displayFormat;
 97             }
 98             set
 99             {
100                 displayFormat = value;
101             }
102         }
103 
104 
105         double maxValue = double.MaxValue;
106         [DefaultValue(double.MaxValue), Description("Indicates maximum value that can be entered.")]
107         public double MaxValue
108         {
109             get
110             {
111                 return this.maxValue;
112             }
113             set
114             {
115                 this.maxValue = value;
116             }
117         }
118 
119         double minValue = double.MinValue;
120         [Description("Indicates minimum value that can be entered."), DefaultValue(double.MinValue)]
121         public double MinValue
122         {
123             get
124             {
125                 return this.minValue;
126             }
127             set
128             {
129                 this.minValue = value;
130             }
131         }
132 
133 
134         #endregion -- 属性 --
135 
136         /// <summary>
137         /// Required designer variable.
138         /// </summary>
139         private System.ComponentModel.Container components = null;
140 
141         private void Init()
142         {
143             InitializeComponent();
144             this.BorderStyle = BorderStyle.FixedSingle;
145             this.TextAlign = HorizontalAlignment.Right;
146         }
147 
148         public SimNumericTextBox(System.ComponentModel.IContainer container)
149         {
150             container.Add(this);
151 
152             Init();
153         }
154 
155         public SimNumericTextBox()
156         {
157             Init();
158         }
159 
160         #region Component Designer generated code
161         /// <summary>
162         /// Required method for Designer support - do not modify
163         /// the contents of this method with the code editor.
164         /// </summary>
165         private void InitializeComponent()
166         {
167             components = new System.ComponentModel.Container();
168         }
169         #endregion
170 
171 
172         //ProcessCmdKey
173         protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
174         {
175             //return base.ProcessCmdKey(ref msg, keyData);
176             if (keyData == (Keys)Shortcut.CtrlV) // 快捷键 Ctrl+V 粘贴操作
177             {
178                 string text = Clipboard.GetText();
179                 this.Text = text;
180                 return true;
181             }
182             if (keyData == (Keys)Shortcut.Del)
183             {
184                 if (base.SelectionStart < this.Text.Length && Text.Substring(base.SelectionStart, 1) == ".")
185                 {//在小数点的前面点击del
186                     return true;
187                 }
188 
189                 if (base.SelectionStart < this.Text.Length && this.SelectedText != null && this.SelectedText.Length > 0)
190                 {
191                     int start = base.SelectionStart;
192                     string previewText = this.Text.Substring(0base.SelectionStart) + this.Text.Substring(base.SelectionStart + this.SelectedText.Length);
193                     this.Text = previewText;
194                     LoadValue();
195                     return true;
196                 }
197                 LoadValue();
198             }
199             return base.ProcessCmdKey(ref msg, keyData);
200         }
201 
202         protected override void OnKeyUp(KeyEventArgs e)
203         {
204             LoadValue();
205             base.OnKeyUp(e);
206         }
207 
208         ///失去焦点时格式化数据,复制进来的数据,如果非正常数字格式,则为0
209         protected override void OnLostFocus(EventArgs e)
210         {
211             LoadValue();
212             //this.Select(this.Text.Length, 0);
213 
214             base.OnLostFocus(e);
215         }
216 
217 
218 
219         ///处理按键事件
220         protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
221         {
222             //e.Handled = true;//true输入无效  false能输入
223             //return;
224             int ch = (int)e.KeyChar;
225             e.Handled = true;
226             int startBase = base.SelectionStart;
227             /// 处理负号
228             if (ch == 45)
229             {
230 
231                 if (this.Text == "")
232                     e.Handled = false;
233                 else if (!this.Text.StartsWith("-"))
234                 {
235                     /// 没有负号
236                     this.Text = "-" + this.Text;
237                     this.Select(startBase + 10);
238                 }
239                 else
240                 {
241                     /// 有负号
242                     this.Text = this.Text.Remove(01);
243                     if (startBase > 0)
244                         this.Select(startBase - 10);
245                     else
246                         this.Select(00);
247                 }
248                 return;
249             }
250 
251             /// 数字,Backspace
252             if ((ch > 47 && ch < 58) || ch == 8)
253             {
254                 e.Handled = false;
255 
256                 if (this.Text == "" || this.Text == "-")
257                 {
258                     return;
259                 }
260                 /*
261                 if (this.Text == "0" || this.Text == "-0")
262                 {
263                     if (ch == 48)
264                         e.Handled = true;
265                     else
266                     {
267                         if (this.Text.StartsWith("-"))
268                             this.Text = "-";
269                         else
270                             this.Text = "";
271                         this.Select(this.Text.Length, 0);
272                     }
273                     return;
274                 }
275                 if (this.Text == "0." || this.Text == "-0.")
276                 {
277                     return;
278                 }
279                 */
280 
281 
282                 if (ch == 8)//退格键
283                 {
284                     if (base.SelectionStart == (Text.IndexOf(".") + 1))
285                     {//在小数点的后面点击退格键
286                         e.Handled = true;
287                         return;
288                     }
289                 }
290                 else
291                 {
292                     if (base.SelectionStart <= this.Text.Length)
293                     {
294                         int start = this.SelectionStart;
295                         string previewText = this.Text.Substring(0base.SelectionStart) + e.KeyChar.ToString() + this.Text.Substring(base.SelectionStart + this.SelectedText.Length);
296                         this.Text = previewText;
297                         if (previewText.IndexOf("0") == 0)
298                         {
299                             this.Select(start, 0);
300                         }
301                         else
302                         {
303                             this.Select(start + 10);
304                         }
305                         /*double oldValue = 0;
306                         double isValue = 0;
307                         double.TryParse(previewText, out isValue);
308                         double.TryParse(this.Text, out oldValue);
309 
310                         if (isValue != 0)
311                         {
312                             this.Text = isValue.ToString(displayFormat);
313                             if (oldValue != isValue)
314                                 this.Select(start + 1, 0);
315                             else
316                                 this.Select(start, 0);
317                         }*/
318                         e.Handled = true;
319                     }
320                 }
321                 return;
322             }
323 
324             /// 小数点
325             if (ch == 46)
326             {
327                 if (this.Text.IndexOf(".") < 0)
328                     e.Handled = false;
329                 else
330                 {
331                     this.SelectionStart = this.Text.IndexOf(".")+1;
332                     e.Handled = true;
333                 }
334             }
335         }
336 
337         /// <summary>
338         /// 加载值
339         /// </summary>
340         private void LoadValue()
341         {
342             string oldText = this.Text;
343             double v = 0;
344             double.TryParse(this.Text, out v);
345 
346             if (v > maxValue)
347                 v = maxValue;
348             if (v < minValue)
349                 v = minValue;
350             this.Text = v.ToString(displayFormat);
351             if (Text.Length > 0 && (Text == "0" || Text.IndexOf("0.") == 0))//初始输入状态
352             {
353                 this.SelectionStart = 1;
354             }
355             if (ValueChanged != null)
356             {
357                 EventArgs e = new EventArgs();
358                 ValueChanged(this, e);
359             }
360         }
361 
362     }

363 } 

posted @ 2012-08-11 18:04  吴永富  阅读(598)  评论(0编辑  收藏  举报