/[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 904 - (hide annotations) (download)
Wed Sep 17 13:01:31 2014 UTC (6 years, 4 months ago) by william
File size: 7104 byte(s)
+ fix other issues with resizing numeric inputs

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 866 [Browsable(true)]
23     [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
24     [EditorBrowsable(EditorBrowsableState.Always)]
25 william 903 public override bool AutoSize { get { return base.AutoSize; } set { if (base.AutoSize != value) { base.AutoSize = value; OnAutoSizeChanged(new EventArgs()); } } }
26 william 866
27 william 903 protected override void OnAutoSizeChanged(EventArgs e)
28     {
29     base.OnAutoSizeChanged(e);
30     layout_panel.AutoSize = base.AutoSize;
31     }
32    
33 william 867 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 william 225 private string _Value;
45 william 220 [Browsable(true)]
46 william 225 public string Value
47 william 88 {
48     get { return _Value; }
49 william 904 set { _Value = value; this.Text = value; DoResize(); }
50 william 88 }
51    
52     private bool _ReadOnly;
53 william 220 public bool ReadOnly
54 william 88 {
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 william 223
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 william 225
109     private void txtValueBox_TextChanged(object sender, EventArgs e)
110     {
111     this.Value = this.Text;
112     }
113 william 904 private void DoResize()
114     {
115     using (Graphics g = this.CreateGraphics())
116     {
117     string t = txtValueBox.Text;
118     var size = g.MeasureString(t, this.Font).ToSize();
119     int width = size.Width; // the minimum size of the textbox
120     txtValueBox.Width = width + 5;
121     this.Width = width;
122     }
123     }
124 william 88 }
125    
126     #region NumericBox Extensions
127     public static class NumericBoxExtensions
128     {
129 william 220 public static byte ToByte(this NumericBoxBase val)
130 william 88 {
131     try
132     {
133     return Convert.ToByte(val.Value);
134     }
135     catch { return 0; }
136     }
137 william 220 public static sbyte ToSByte(this NumericBoxBase val)
138 william 88 {
139     try
140     {
141     return Convert.ToSByte(val.Value);
142     }
143     catch { return 0; }
144     }
145 william 220 public static Int16 ToInt16(this NumericBoxBase val)
146 william 88 {
147     try
148     {
149     return Convert.ToInt16(val.Value);
150     }
151     catch { return 0; }
152     }
153 william 220 public static Int32 ToInt32(this NumericBoxBase val)
154 william 88 {
155     try
156     {
157     return Convert.ToInt32(val.Value);
158     }
159     catch { return 0; }
160     }
161 william 220 public static Int64 ToInt64(this NumericBoxBase val)
162 william 88 {
163     try
164     {
165     return Convert.ToInt64(val.Value);
166     }
167     catch { return 0; }
168     }
169 william 220 public static UInt16 ToUInt16(this NumericBoxBase val)
170 william 88 {
171     try
172     {
173     return Convert.ToUInt16(val.Value);
174     }
175     catch { return 0; }
176     }
177 william 220 public static UInt32 ToUInt32(this NumericBoxBase val)
178 william 88 {
179     try
180     {
181     return Convert.ToUInt32(val.Value);
182     }
183     catch { return 0; }
184     }
185 william 220 public static UInt64 ToUInt64(this NumericBoxBase val)
186 william 88 {
187     try
188     {
189     return Convert.ToUInt64(val.Value);
190     }
191     catch { return 0; }
192     }
193 william 220 public static Single ToSingle(this NumericBoxBase val)
194 william 88 {
195     try
196     {
197     return Convert.ToSingle(val.Value);
198     }
199     catch { return 0; }
200     }
201 william 220 public static double ToDouble(this NumericBoxBase val)
202 william 88 {
203     try
204     {
205     return Convert.ToDouble(val.Value);
206     }
207     catch { return 0; }
208     }
209 william 220 public static decimal ToDecimal(this NumericBoxBase val)
210 william 88 {
211     try
212     {
213     return Convert.ToDecimal(val.Value);
214     }
215     catch { return 0; }
216     }
217 william 220 public static string ToString(this NumericBoxBase val)
218 william 88 {
219     try
220     {
221     return Convert.ToString(val.Value);
222     }
223     catch { return "0"; }
224     }
225     }
226     #endregion
227     }

  ViewVC Help
Powered by ViewVC 1.1.22