Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

HandlEdInput - Powerful and Highly Customizable Input Handler for TextBox, RichTextBox and ComboBox

0.00/5 (No votes)
17 Jul 2019 1  
Handles and validates input typing and pressed keys in TextBox, RichTextBox and ComboBox, displaying custom balloon tips messages

Preview

Introduction

HandlEdInput handles and validates input typing and pressed keys on TextBox, RichTextBox and ComboBox, displaying custom balloon tips messages.

The benefits offered are huge, thanks to its simplicity of use, plus the highly customizable properties, not only blocks input typing and pressed keys but also validate by using regular expressions while in ComboBox can also validate if the typed input is on the item list or not, including many more features.

This is a list of what it can do.

Handling (Controlling the Input)

  • Block alpha from input typing
  • Block numeric from input typing
  • Block non-alphanumeric from input typing
  • Block one, some or all of the above
  • Allow only specific characters and deny the rest from input typing
  • Deny only specific characters and allow the rest from input typing
  • Block specific keys from taking effect, i.e., blocking space or backspace keys
  • Block any keyboard shortcuts in controls, as well as the context menu
  • Allow one single spacing per character only
  • Disable spacing at the very first beginning of the input
  • Validate input via regular expression patterns
  • Validate ComboBox input from being on the item list or not
  • NEW! Handle paste command
  • NEW! Disable text selection
  • NEW! Handle text drop

Messages (Displaying Custom Balloon Tips at Every Detected Action)

  • Set the title of message
  • Set the content of message
  • Choose between Info, Error, Warning or None icons
  • Use parameters inside the content message which are replaced in the fly, i.e., "You pressed {KeyData}"
  • Change default language of message presets
  • Change timeout of balloon tips
  • Set the visibility of any shown balloon tip case
  • Hide all balloon tips
  • And more...

The best part is that all of these settings can be combined in a very easy way.

For example, let's say you want to capture a phone number.

  1. You can easily begin blocking anything other than numbers.
  2. Then you can include the characters: + ( ) -
  3. In order to validate the input, you can include a regular expression pattern.
  4. You can set to allow one single spacing per character.
  5. Finally, you can customize the balloon tips when there's a regular expression mismatch pattern, or match pattern.

Background

I started this for personal use, I needed some robust input typing handler for a project I was working on instead of writing the necessary code only for input blocking. So I thought it would be useful to have a generic, flexible and complete solution to restrict and control the input typing, that's how this project was born. At first, it was a very simple code and met my needs at that time. But after some days, I decided to continue my work on this, thinking about new and useful ideas that could be incorporated, and after some weeks of work... now it's ready to be shared.

Previews

Preview

Preview

Preview

Using the Code

First, you have to pass the TextBox, RichTextBox or ComboBox control on class instance declaration.

Handled.Input T1 = new Handled.Input(textBox1);

(Tip: You can also pass more than one control separated by commas if you want more than one to behave the same).

Then you can use the instance object of the class to begin the setup.

Handled.Input T1 = new Handled.Input(textBox1);
T1.Handler.Settings.Set_BlockadeMode = true;
T1.Handler.Handle_Numeric = true;
T1.Handle();

The given example is a simple demonstration on how to allow only numbers as input.

Let's Get Into a Little Bit More Advanced Control Handling

Handled.Input T2 = new Handled.Input(textBox2);
T2.Handler.Handle_NonAlphanumeric = true;
T2.Handler.List_IncludeChars = new List<char> { '_', '-', '.' };
T2.Handler.List_RestrictKeys = new List<string> { "Space" };
T2.Message.OnFocus.Set_Title = "Please enter a username";
T2.Message.OnFocus.Set_Message = 
"Username can only contain {tokens} including the following characters:\n{include_chars}";
T2.Message.OnInvalidInput.Set_Icon = Handled.BalloonTip.ICON.ERROR;
T2.Message.OnInvalidInput.Set_Title = "Invalid";
T2.Message.OnInvalidInput.Set_Message = "Enter a valid input.";
T2.Message.OnIncludeChars.Set_Visible = false;
T2.Message.Settings.Set_AllTimeouts = 5000;
T2.Handle();

Once the class instance is declared, passing the control to handle:

  1. Blocks any non-alphanumeric character
  2. Includes only the following characters: _ -
  3. Restricts any possible whitespace input
  4. Sets a message title and its content for the balloon tip focus message
  5. Sets a custom icon, the message title and the content for the balloon tip invalid message
  6. Sets the included characters message visibility to hidden
  7. Extends the balloon tips timeout to 5 seconds
  8. Finally confirms to handle the control

(Note: The order of setting doesn't matter as long as it begins with the class instance declaration and finalizes with the Handle() method).

A More Advanced Example Would Be Including a Regular Expression Validation

Handled.Input T3 = new Handled.Input(textBox3);
T3.Handler.Settings.Set_BlockadeMode = true;
T3.Handler.Handle_Alpha = true;
T3.Handler.List_RestrictKeys = new List<string> { "Space" };
T3.Handler.Settings.Set_RegexMatchPattern = "^(?=[MDCLXVI])M*D?C{0,4}L?X{0,4}V?I{0,4}$";
T3.Handler.Settings.Set_RegexOptions = 
System.Text.RegularExpressions.RegexOptions.IgnoreCase;
T3.Handler.Settings.Set_ShortcutsEnabled = false;
T3.Message.OnFocus.Set_Message = "Enter a roman number.";
T3.Message.OnRegexMismatch.Set_Message = "Type a valid roman number.";
T3.Message.OnRegexMatch.Set_Message = "The roman number is valid.";
T3.Message.OnRestrictKeys.Set_Visible = false;
T3.Handle();

Once the class instance is declared, passing the control to handle:

  1. Enables blockade mode which means it will invert handling and start disabling all character types (alpha, numeric and non-alphanumeric)
  2. Allows only alpha characters
  3. Restricts any possible whitespace input
  4. Sets a regular expression pattern to only match valid roman numbers
  5. Indicates the regular expression to behave as no case sensitive (this can also be set in the regular expression pattern, but the purpose in this case is to show the regular expression options property)
  6. Disables any shortcut combination inside the control (Ctrl+A, Ctr+C, Ctrl+V..) disabling also its context menu
  7. Sets the content for the balloon tip focus message
  8. Sets the content for the balloon tip regular expression mismatch message
  9. Sets the content for the balloon tip regular expression match message
  10. Hides the visibility of the restrict keys balloon tip message when a character other than alpha is detected
  11. Finally confirms to handle the control

Now Let's See A Simple ComboBox Validation Example

Handled.Input C1 = new Handled.Input(comboBox1);
C1.Handler.Settings.Set_BlockadeMode = true;
C1.Handler.Handle_Numeric = true;
C1.Handler.Settings.Set_ComboBoxValidator = true;  
C1.Message.OnFocus.Set_Title = "Set Day";
C1.Message.OnFocus.Set_Message = "Please set the day here.";
C1.Message.OnInvalidInput.Set_Message = "Set a valid day.";
C1.Message.OnComboBoxInvalidInput.Set_Message = "Set a valid day of the month.";
C1.Message.OnComboBoxValidInput.Set_Message = "The day is valid now.";
C1.Handle();

Once the class instance is declared, passing the control to handle:

  1. Enables blockade mode, disabling all characters input
  2. Allows only numeric input
  3. Enables the ComboBox validator, which means it will look for the entered input on the items list and validate
  4. Sets the title for the balloon tip focus message
  5. Sets the content for the balloon tip focus message
  6. Sets the content for the balloon tip invalid input message (i.e., trying to enter an alpha character)
  7. Sets the content for the ComboBox balloon tip invalid input message (i.e., on valid numeric characters but not in the ComboBox item list)
  8. Sets the content for the ComboBox balloon tip valid input message
  9. Finally confirms to handle the control

Message Parameters

You can use a set of parameters for the balloon tip messages.
These parameters would be replaced at real time with some useful information, for example, to let a user know at the very first time which is the valid input to type over a control, you would use in the focus message something like: "Input can only contain {tokens}, including the following characters:\n{include_chars}."
The parameters available to use are the following:

  • {tokens} = Displays the allowed tokens
  • {KeyChar} = Displays the pressed character
  • {KeyData} = Displays the name of pressed key
  • {exclude_chars} = Displays the list of the exclude characters
  • {include_chars} = Displays the list of the include characters
  • {restrict_keys} = Displays the list of the restrict keys

Recommendation

Optional custom controls were added. These custom controls can now completely handle all input attempts by supporting text Drop and Paste handling on both ways (keyboard shortcut and context menu).
For a complete input handling, it's recommended to use these custom controls.
You'll find these on the Designer Toolbox. Once you add them to your UI, you're good to go for customizing their behavior in order to fit your needs.

Credits

Big thanks to Chris and Ivan Petrov for providing code that made it possible for me to make this project grow, more details inside the code.

History

  • 07/17/2019
    • Introduced text Drop validation support on HandledTextBox, HandledRichTextBox and HandledComboBox controls! (Requires "Set_DropValidationOnCustomControls" to be enabled, this automatically sets "AllowDrop"/"EnableAutoDragDrop" properties as well)
    • Added RTF format support when text Paste/Drop on HandledRichTextBox and doesn't require to reformat whitespaces/breaklines
    • Renamed properties. ("Is_RegexValidMatch" / "Is_ComboBoxInputOnList")
  • 07/01/2019
    • Fixed minor bug (There was a specific way that allowed to paste a whitespace next to another one on single spacing handling)
    • Minor camelcase naming changes in some properties
  • 06/29/2019 - HandlEdInput gets even more powerful!
    • Added custom controls supporting Paste command handling!
    • Performance improvements!
    • Now it is easier to change the default balloon tip messages language and also add your custom language inside Handled.Input class.
    • Now it is not necessary to also pass "Control"/"Shift" variants (i.e., "Space, Control", "Space, Shift") to the restrict keys list in order to avoid any possible way to bypass the restricted key. Instead, just pass the normal case (i.e. "Space").
    • Besides restricting "Space" on restrict keys list, now you can also add a whitespace character to the exclude list to avoid spacing.
    • Added individual timeout properties to all types of balloon tip messages (Having the possibility to global set all timeouts on message settings)
    • On focus balloon tip, messages are now visible only when input is empty (Still can show it manually anytime by pressing "F1")
    • Added "Set_ContextMenuOnCustomControls" to define whether to set a context menu on HandledTextBox, HandledRichTextBox and HandledComboBox. (Default: True)
    • Added "Set_ContextMenuShortcutsOnCustomControls" to define whether to set the commands shortcuts as visible on HandledTextBox, HandledRichTextBox and HandledComboBox context menu. (Default: True)
    • Added "Handle_TextSelectionOnCustomTextBox" to define whether the text can be selected on HandledTextBox (automatically adds text selection keyboard shortcuts to restrict keys list)
    • Added "Handle_TextSelectionOnCustomRichTextBox" to define whether the text can be selected on HandledRichTextBox (automatically adds text selection keyboard shortcuts to restrict keys list)
    • Added "IsRegexValidMatch" to programmatically consult whether a regular expression pattern is matching!
    • Added "IsComboBoxInputOnList" to programmatically consult whether the current input is listed on the ComboBox list!
    • Disabling button events while an input is not valid is now only applicable if the respective balloon tip invalid message is visible
    • "Handle_BeginSpace" and "Handle_OneSingleSpacing" is now more powerful and intelligent when characters are being erased!
    • Fixed not hiding the balloon tips on losing control focus (The balloon tip was kept visible even after minimizing the window)
  • 01/22/2017 - First release

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here