ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/RomCheater/trunk/Win32/libWin32/Win32/UI/NumericBoxBase.cs
Revision: 225
Committed: Sat Jun 2 14:29:00 2012 UTC (11 years ago) by william
File size: 5859 byte(s)
Log Message:
+ commit wip for data type conversion: need to fix format exceptions

File Contents

# Content
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 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 }
92
93 #region NumericBox Extensions
94 public static class NumericBoxExtensions
95 {
96 public static byte ToByte(this NumericBoxBase val)
97 {
98 try
99 {
100 return Convert.ToByte(val.Value);
101 }
102 catch { return 0; }
103 }
104 public static sbyte ToSByte(this NumericBoxBase val)
105 {
106 try
107 {
108 return Convert.ToSByte(val.Value);
109 }
110 catch { return 0; }
111 }
112 public static Int16 ToInt16(this NumericBoxBase val)
113 {
114 try
115 {
116 return Convert.ToInt16(val.Value);
117 }
118 catch { return 0; }
119 }
120 public static Int32 ToInt32(this NumericBoxBase val)
121 {
122 try
123 {
124 return Convert.ToInt32(val.Value);
125 }
126 catch { return 0; }
127 }
128 public static Int64 ToInt64(this NumericBoxBase val)
129 {
130 try
131 {
132 return Convert.ToInt64(val.Value);
133 }
134 catch { return 0; }
135 }
136 public static UInt16 ToUInt16(this NumericBoxBase val)
137 {
138 try
139 {
140 return Convert.ToUInt16(val.Value);
141 }
142 catch { return 0; }
143 }
144 public static UInt32 ToUInt32(this NumericBoxBase val)
145 {
146 try
147 {
148 return Convert.ToUInt32(val.Value);
149 }
150 catch { return 0; }
151 }
152 public static UInt64 ToUInt64(this NumericBoxBase val)
153 {
154 try
155 {
156 return Convert.ToUInt64(val.Value);
157 }
158 catch { return 0; }
159 }
160 public static Single ToSingle(this NumericBoxBase val)
161 {
162 try
163 {
164 return Convert.ToSingle(val.Value);
165 }
166 catch { return 0; }
167 }
168 public static double ToDouble(this NumericBoxBase val)
169 {
170 try
171 {
172 return Convert.ToDouble(val.Value);
173 }
174 catch { return 0; }
175 }
176 public static decimal ToDecimal(this NumericBoxBase val)
177 {
178 try
179 {
180 return Convert.ToDecimal(val.Value);
181 }
182 catch { return 0; }
183 }
184 public static string ToString(this NumericBoxBase val)
185 {
186 try
187 {
188 return Convert.ToString(val.Value);
189 }
190 catch { return "0"; }
191 }
192 }
193 #endregion
194 }