• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1910
  • Last Modified:

MaskedTextBox

Dear Experts

i am asking about a mask that take only HEX.
and how to make this MaskedTextBox Upper Case
thank you
0
RamzyEbeid
Asked:
RamzyEbeid
2 Solutions
 
CraigHarrisCommented:
There is no masking character for HEX
For uppercase, put a > at the start of the mask.

For more info in VS, visit ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETDEVFX.v20.en/CPref17/html/P_System_Windows_Forms_MaskedTextBox_Mask.htm

You can accomplish your requirement using either a MaskedTextBox or a standard TextBox by handling the KeyPress events, the method you need is:

    private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e) {
      String key = e.KeyChar.ToString().ToUpper();
      char[] permittedKeys = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
      if ( key.IndexOfAny(permittedKeys) == -1 ) {
        e.Handled = true;
      } else {
        e.KeyChar = key[0];
      }
    }

Please post back if you require further explanation - I've tested this in Visual Studio 2005 and it works fine.
0
 
Juser7658Commented:
However, while that would work, other text could potentially be pasted into the textbox.
Better would be to put code in TextBox1_Change (or whatever it is) that checks each character and removes any that are not valid hex characters.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now