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

Diff of /trunk/Win32/libWin32/Win32/UI/MaskedHexBox.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/Win32/libWin32/Win32/UI/MaskedHexBox.cs	2013/06/17 02:25:07	661
+++ trunk/Win32/libWin32/Win32/UI/MaskedHexBox.cs	2013/06/17 02:54:52	662
@@ -13,9 +13,6 @@
     [DefaultEvent("ValueChanged")]
     public partial class MaskedHexBox : UserControl
     {
-        const int ReferenceWidthPixels = 114;
-        const double ReferenceWidthPercent = 0.50;
-
         public MaskedHexBox()
         {
             InitializeComponent();
@@ -82,7 +79,7 @@
         public override string Text
         {
             get { return txtMaskedHexBox.Text.PadRight(this.TextLength + 2, '0'); }
-            set { txtMaskedHexBox.Text = value; }
+            set { txtMaskedHexBox.Text = value; DoResize(); }
         }
         public ulong Value
         {
@@ -170,6 +167,7 @@
                     }
                 }
                 this.Mask = mask.ToString();
+                DoResize();
             }
         }
         private string CreateMask(int length)
@@ -200,7 +198,58 @@
 
         }
 
+        private void MaskedHexBox_Load(object sender, EventArgs e)
+        {
+            DoResize();
+        }
+
+        protected override void OnResize(EventArgs e)
+        {
+            base.OnResize(e);
+            DoResize();
+        }
+
+        private void DoResize()
+        {
+            string t = string.Empty;
+            switch (this.TextLength)
+            {
+                case 2:
+                    t = string.Format("0x{0:x2}", this.Value);
+                    break;
+                case 4:
+                    t = string.Format("0x{0:x4}", this.Value);
+                    break;
+                case 8: goto default;
+                case 16:
+                    t = string.Format("0x{0:x16}", this.Value);
+                    break;
+                default: 
+                     t = string.Format("0x{0:x8}", this.Value);
+                     break;
+            }
+
+
+
+            using (Graphics g = this.CreateGraphics())
+            {
+                var size = g.MeasureString(t, this.Font).ToSize();
+                int width = size.Width; // the minimum size of the textbox
+                double growth = (double)size.Width / 0.5;
+
+                switch (this.TextLength)
+                {
+                    case 2: this.Width = (int)growth + 25; break;
+                    case 4: this.Width = (int)growth + 15; break;
+                    case 8: goto default;
+                    case 16: this.Width = (int)growth - 70; break;
+                    default:
+                        this.Width = (int)growth - 15; break;
+                }
 
+                
+            }
+        }
     }
 
     #region MaskedHexBox Extensions

 

  ViewVC Help
Powered by ViewVC 1.1.22