/[OhmsLawCalc]/trunk/OhmsLawCalc/UI/NumericBoxBase.cs
ViewVC logotype

Annotation of /trunk/OhmsLawCalc/UI/NumericBoxBase.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 22 - (hide annotations) (download)
Wed Jan 29 07:57:25 2014 UTC (6 years ago) by william
File size: 6308 byte(s)

1 william 18 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     using System.Collections;
10    
11     namespace System.Windows.Forms
12     {
13     public partial class NumericBoxBase : UserControl
14     {
15     public NumericBoxBase()
16     {
17     InitializeComponent();
18     this.Value = (0).ToString();
19     this.ReadOnly = false;
20     }
21    
22     private string _Value;
23     [Browsable(true)]
24     public string Value
25     {
26     get { return _Value; }
27     set { _Value = value; this.Text = value; }
28     }
29    
30     private bool _ReadOnly;
31     public bool ReadOnly
32     {
33     get { return _ReadOnly; }
34     set { _ReadOnly = value; if (_ReadOnly) { txtValueBox.Enabled = false; } else { txtValueBox.Enabled = true; } }
35     }
36     public override string Text
37     {
38     get { return txtValueBox.Text; }
39     set { txtValueBox.Text = value; }
40     }
41    
42     private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text, this.Text); }
43     private void btnPaste_Click(object sender, EventArgs e) { if (this.ReadOnly) return; this.Text = (string)Clipboard.GetData(DataFormats.Text); }
44    
45    
46     protected void CreateStandardKeys(List<Keys> ValidKeys)
47     {
48     // these are mostly editing/navigation keys
49     ValidKeys.Add(Keys.Delete);
50     ValidKeys.Add(Keys.Back);
51     ValidKeys.Add(Keys.Up);
52     ValidKeys.Add(Keys.Down);
53     ValidKeys.Add(Keys.Left);
54     ValidKeys.Add(Keys.Right);
55     ValidKeys.Add(Keys.Home);
56     ValidKeys.Add(Keys.End);
57     }
58     protected virtual void CreateValidKeys(List<Keys> ValidKeys)
59     {
60     //ValidKeys.AddRange(Enum.GetValues(typeof(Keys)).Cast<Keys>().Where(x => x >= Keys.D0 && x <= Keys.D9));
61     // ValidKeys.AddRange(Enum.GetValues(typeof(Keys)).Cast<Keys>().Where(x => x >= Keys.NumPad0 && x <= Keys.NumPad9));
62     }
63     protected List<Keys> GetValidKeys()
64     {
65     List<Keys> ValidKeys = new List<Keys>();
66     CreateStandardKeys(ValidKeys);
67     CreateValidKeys(ValidKeys);
68     return ValidKeys;
69     }
70     private void txtValueBox_KeyDown(object sender, KeyEventArgs e)
71     {
72     List<Keys> ValidKeys = this.GetValidKeys();
73     if (ValidKeys.Contains(e.KeyCode))
74     {
75     if (txtValueBox.Text.Contains(".") && (e.KeyCode == Keys.Decimal || e.KeyCode == Keys.OemPeriod))
76     {
77     e.SuppressKeyPress = true;
78     }
79     return;
80     }
81     if(!e.Control && !e.Alt)
82     {
83     e.SuppressKeyPress = true;
84     }
85     }
86    
87     private void txtValueBox_TextChanged(object sender, EventArgs e)
88     {
89     this.Value = this.Text;
90     }
91 william 22
92     //private void InitializeComponent()
93     //{
94     // this.SuspendLayout();
95     // //
96     // // NumericBoxBase
97     // //
98     // this.AutoSize = true;
99     // this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
100     // this.Name = "NumericBoxBase";
101     // this.Size = new System.Drawing.Size(0, 0);
102     // this.ResumeLayout(false);
103    
104     //}
105 william 18 }
106    
107     #region NumericBox Extensions
108     public static class NumericBoxExtensions
109     {
110     public static byte ToByte(this NumericBoxBase val)
111     {
112     try
113     {
114     return Convert.ToByte(val.Value);
115     }
116     catch { return 0; }
117     }
118     public static sbyte ToSByte(this NumericBoxBase val)
119     {
120     try
121     {
122     return Convert.ToSByte(val.Value);
123     }
124     catch { return 0; }
125     }
126     public static Int16 ToInt16(this NumericBoxBase val)
127     {
128     try
129     {
130     return Convert.ToInt16(val.Value);
131     }
132     catch { return 0; }
133     }
134     public static Int32 ToInt32(this NumericBoxBase val)
135     {
136     try
137     {
138     return Convert.ToInt32(val.Value);
139     }
140     catch { return 0; }
141     }
142     public static Int64 ToInt64(this NumericBoxBase val)
143     {
144     try
145     {
146     return Convert.ToInt64(val.Value);
147     }
148     catch { return 0; }
149     }
150     public static UInt16 ToUInt16(this NumericBoxBase val)
151     {
152     try
153     {
154     return Convert.ToUInt16(val.Value);
155     }
156     catch { return 0; }
157     }
158     public static UInt32 ToUInt32(this NumericBoxBase val)
159     {
160     try
161     {
162     return Convert.ToUInt32(val.Value);
163     }
164     catch { return 0; }
165     }
166     public static UInt64 ToUInt64(this NumericBoxBase val)
167     {
168     try
169     {
170     return Convert.ToUInt64(val.Value);
171     }
172     catch { return 0; }
173     }
174     public static Single ToSingle(this NumericBoxBase val)
175     {
176     try
177     {
178     return Convert.ToSingle(val.Value);
179     }
180     catch { return 0; }
181     }
182     public static double ToDouble(this NumericBoxBase val)
183     {
184     try
185     {
186     return Convert.ToDouble(val.Value);
187     }
188     catch { return 0; }
189     }
190     public static decimal ToDecimal(this NumericBoxBase val)
191     {
192     try
193     {
194     return Convert.ToDecimal(val.Value);
195     }
196     catch { return 0; }
197     }
198     public static string ToString(this NumericBoxBase val)
199     {
200     try
201     {
202     return Convert.ToString(val.Value);
203     }
204     catch { return "0"; }
205     }
206     }
207     #endregion
208     }

  ViewVC Help
Powered by ViewVC 1.1.22