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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 223 - (show annotations) (download)
Sat Jun 2 13:20:13 2012 UTC (8 years, 3 months ago) by william
File size: 5728 byte(s)
+ add support for multi input box : octal is not setup

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

  ViewVC Help
Powered by ViewVC 1.1.22