Windows Develop Bookmark and Share   
 index > Windows Forms General > Customising the TextBox
 

Customising the TextBox

Hi,

I would like to extend the TextBox so that I can enter numeric only values. How can I extend the CharacterCasing property to include a new'numeric' enum?

BTW I am using C# in VS2005

Regards

Ron.

ron2464  Friday, October 26, 2007 9:00 PM
Hi Ron,

You cannot add another value to the CharacterCasing enumeration because you don't have the source for it. Instead, you could extend TextBox and add your own property that controls the allowed input.

Also, you might try searching for existing implementations. I'm quite sure this problem would have been solved many times over.

HTH,
Kent
Kent Boogaart  Monday, October 29, 2007 3:35 PM
add the textbox KeyPress event and paste this code in it:
if (!char.IsNumber(e.KeyChar))
e.Handled = true;
Cameron Kloot  Friday, October 26, 2007 9:20 PM
As people have often mentioned, handling KeyPress event is not foolproof. Someone can copy-paste text into the textbox and this logic will fail.

I usually handle the Leave event.

TextBox_Leave()
{
// Validate input
}

I see two advantages:

1. The event is not triggered every time the user enters a character.
2. It works for any kind of input device - keyboard or mouse.

I see one major problem with this, though:

If your form has its AcceptButton set to any button, and the user presses "Enter" after entering some text into the TextBox, the Leave event is never triggered and the form is closed. Either you can disable the AcceptButton (if you have many text boxes to validate) or validate the text again in 'Form_Closing' event.

This solution has worked well for me until now.
J Andrews  Saturday, October 27, 2007 8:48 AM

Thanks for the suggestions with regards to the actual logic, but my main problem is how to extend the TextBox CharacterCasing property, so that the code can determine what type of characters are t obe allowed, e.g. numerics or upper/lower/normal text?

Regards

Ron.

ron2464  Saturday, October 27, 2007 10:54 AM
ron2464 wrote:

so that the code can determine what type of characters are t obe allowed, e.g. numerics or upper/lower/normal text?



what do you mean by this? if not what i posted erlier? please give an example.
Cameron Kloot  Saturday, October 27, 2007 12:03 PM
Hi Cameron,

Firstly, sorry for the delay, but there was a problem with the forum yesterday and I couldn't respond to you :-(

Any, one of the properties of a TextBox is CharacterCasing, which can be one of three values: Normal, Upper or Lower. So my plan was to derive a new class (e.g. MyTextBox) from the TextBox class and then add another value to the CharacterCasing property, so that the property now had four values: Normal, Upper, Lower and Numeric. I would then add the logic to allow only numeric values (if Numeric was selected).



Hope that makes sense.



Regards



Ron.



ron2464  Sunday, October 28, 2007 9:41 AM
Hi Ron,

You cannot add another value to the CharacterCasing enumeration because you don't have the source for it. Instead, you could extend TextBox and add your own property that controls the allowed input.

Also, you might try searching for existing implementations. I'm quite sure this problem would have been solved many times over.

HTH,
Kent
Kent Boogaart  Monday, October 29, 2007 3:35 PM

Hi Kent,

Thanks for that. It is sort of what I figured, but I was still hoping as it does seem to be somewhat of an oversight, as this facility has always been available in the previous MS development products that I have used in the past :-(

Thanks again.

Ron.

ron2464  Tuesday, October 30, 2007 9:37 PM

You can use google to search for other answers

Custom Search

More Threads

• SqlConnection and Dispose in Windows Forms
• 2 Listviews: horizontal scroll
• two questions...
• Panel border color?
• Can I link List<T> to DataGridView?
• How to schedule a .Net windows service using win scheduled tasks service ?
• Regards checkbox in datagridview
• Form GUI information not loading
• Locking an Application
• Hierarchical combo box