Code Red > .NET

Getting specific type of user input

(1/2) > >>

Jeff_M:
I think that I am doing what I usually do, overthink the issue. I could use a hand with this....I need to get user input of Point Numbers. These will always be positive and non-zero. The user may respond with just a single number, or multiple numbers separated by a comma, or a range of numbers separated by a hyphen. So any of these scenarios are valid:
23
23,28,25
23-25
20,23-25,46,50,100-123

I don't believe that the GetInteger() method will work since it's a string being returned. So I need to use GetString() with the PromptStringOptions property Allow spaces set to false. My question is, how do I force the input to be in the correct format? Only allow positive integers, separated by a single comma, and/or positive integers separated by a single hyphen, and disallow all other input (other than ENTER or ESC)?

I'm hoping that I've just overlooked something relatively simple and you all can show me the path. Thanks!

kdub_nz:

I think you'll need to build your own parser for the string Jeff.

Using a tilde ( ~) for range may save some confusion asserting no negative numbers.

Do you want the return to be a list of integers ?

I assume you want duplicates removed from the final result ?

I'd be tempted to just ignore any minus characters ( perhaps advise the user you have done so ... " naughty girl, do better next time" )

Atook:
I think parsing a string is the way to go. To filter the input as the user is entering, I can think of two options:


* Build a little dialog with text box, and filter on the TextChanged event.
* Add/Remove an IMessageFilter to the Application while receiving input and filter bad input there.I'd opt for the first.

I'd imagine there's a regex expression you could use for filtering the string, though regex is above my paygrade.

There might be a cleaner way to do it, but someone else would will to chime in with it.

kdub_nz:

The parser is fairly simple.
Split the input string on ','

iterate the result of split
If the item contains a tilde , split the item.
   assert first and last are numeric
   build a range from abs(first) to abs(last)
   add this to result list.

else
   assert item is numeric
   add abs(item) to result list
   add abs

Jeff_M:
Thanks, guys. Kerry, I already have a parser which works great to convert the string to an array of integers. My desire is to insure the string is created properly as the user enters the numbers of interest. Atook, I was hoping to keep it all on the command line, but had considered using a form so I can easily test the keypress event for either a digit, comma, or hyphen being entered and whether it is at an applicable location (hyphen can only follow a digit; comma can only follow a digit, both a comma and hyphen must be followed by a digit). So I guess a small form is what is needed.

Navigation

[0] Message Index

[#] Next page

Go to full version