/[RomCheater]/trunk/Win32/libWin32/Win32/UI/NumericBoxBase.cs
ViewVC logotype

Annotation of /trunk/Win32/libWin32/Win32/UI/NumericBoxBase.cs

Parent Directory Parent Directory | Revision Log Revision Log


Revision 225 - (hide annotations) (download)
Sat Jun 2 14:29:00 2012 UTC (9 years, 6 months ago) by william
File size: 5859 byte(s)
+ commit wip for data type conversion: need to fix format exceptions

1 william 88 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 william 223 using System.Collections;
10 william 88
11     namespace System.Windows.Forms
12     {
13 william 220 public partial class NumericBoxBase : UserControl
14 william 88 {
15 william 220 public NumericBoxBase()
16 william 88 {
17     InitializeComponent();
18 william 225 this.Value = (0).ToString();
19 william 88 this.ReadOnly = false;
20     }
21    
22 william 225 private string _Value;
23 william 220 [Browsable(true)]
24 william 225 public string Value
25 william 88 {
26     get { return _Value; }
27 william 225 set { _Value = value; this.Text = value; }
28 william 88 }
29    
30     private bool _ReadOnly;
31 william 220 public bool ReadOnly
32 william 88 {
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 william 223
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 william 225
87     private void txtValueBox_TextChanged(object sender, EventArgs e)
88     {
89     this.Value = this.Text;
90     }
91 william 88 }
92    
93     #region NumericBox Extensions
94     public static class NumericBoxExtensions
95     {
96 william 220 public static byte ToByte(this NumericBoxBase val)
97 william 88 {
98     try
99     {
100     return Convert.ToByte(val.Value);
101     }
102     catch { return 0; }
103     }
104 william 220 public static sbyte ToSByte(this NumericBoxBase val)
105 william 88 {
106     try
107     {
108     return Convert.ToSByte(val.Value);
109     }
110     catch { return 0; }
111     }
112 william 220 public static Int16 ToInt16(this NumericBoxBase val)
113 william 88 {
114     try
115     {
116     return Convert.ToInt16(val.Value);
117     }
118     catch { return 0; }
119     }
120 william 220 public static Int32 ToInt32(this NumericBoxBase val)
121 william 88 {
122     try
123     {
124     return Convert.ToInt32(val.Value);
125     }
126     catch { return 0; }
127     }
128 william 220 public static Int64 ToInt64(this NumericBoxBase val)
129 william 88 {
130     try
131     {
132     return Convert.ToInt64(val.Value);
133     }
134     catch { return 0; }
135     }
136 william 220 public static UInt16 ToUInt16(this NumericBoxBase val)
137 william 88 {
138     try
139     {
140     return Convert.ToUInt16(val.Value);
141     }
142     catch { return 0; }
143     }
144 william 220 public static UInt32 ToUInt32(this NumericBoxBase val)
145 william 88 {
146     try
147     {
148     return Convert.ToUInt32(val.Value);
149     }
150     catch { return 0; }
151     }
152 william 220 public static UInt64 ToUInt64(this NumericBoxBase val)
153 william 88 {
154     try
155     {
156     return Convert.ToUInt64(val.Value);
157     }
158     catch { return 0; }
159     }
160 william 220 public static Single ToSingle(this NumericBoxBase val)
161 william 88 {
162     try
163     {
164     return Convert.ToSingle(val.Value);
165     }
166     catch { return 0; }
167     }
168 william 220 public static double ToDouble(this NumericBoxBase val)
169 william 88 {
170     try
171     {
172     return Convert.ToDouble(val.Value);
173     }
174     catch { return 0; }
175     }
176 william 220 public static decimal ToDecimal(this NumericBoxBase val)
177 william 88 {
178     try
179     {
180     return Convert.ToDecimal(val.Value);
181     }
182     catch { return 0; }
183     }
184 william 220 public static string ToString(this NumericBoxBase val)
185 william 88 {
186     try
187     {
188     return Convert.ToString(val.Value);
189     }
190     catch { return "0"; }
191     }
192     }
193     #endregion
194     }

  ViewVC Help
Powered by ViewVC 1.1.22