how to get numeric input in winForm

S

Sam

Why isn't there a numberBox Windows Form control like a
textbox control but for numeric (float) input? There
doesn't seem to be any easy or visual solution to this
VERY SIMPLE ISSUE in either version of Visual Studio. I
simply wanted to create a Windows app where a user could
enter miles or kilometers, gallons or liters, and see the
other in the adjacent box. The form was easy but the
textbox control would not let me cast any number of any
kind from textbox.text and of course, input validation is
a pain since there is no built-in masking capability.
Validating keystrokes becomes too complex because it
doesn't handle backspace, paste or any other interaction.
Hours of searching help resulted in nothing useful
whatsoever. The Numeric boolean attribute of Textbox
documented in help isn't recognized by Vis Studio. Is
this a bug or just poor documentation?

Must I reinvent the wheel to get a number from a form
control? Please reply with C# code sample, if possible.
 
W

William Ryan

On KeyDown, check the value of e. Here's an example of it. You can create
a custom control and never worry about it again. It's not complex at all.
As a matter of fact, you can even take this code, compile it into your own
control, and be done with it. But don't do that. First read through it ;-)

Cheers,

Bill
 
K

Kieran Benton

This is one of those areas where I really think WinForms is lacking (as you
could quite easily do this back in the MFC days). Maybe they'll extend
textbox in upcoming versions? For now though Ignacio's solution strikes me
as the cleanest :)

Kieran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top