/[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 903 - (show annotations) (download)
Wed Sep 17 12:44:22 2014 UTC (7 years ago) by william
File size: 6676 byte(s)
+ fix some issues with numeric input controls not properly sizing themselves

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

  ViewVC Help
Powered by ViewVC 1.1.22