Recommended Posts

I need to parse a string so that when the user types "Lastname, Firstname" it catches the lastname into one string and the first name into another string - and allows for the user to use the backspace and work properly.
the only thing i could think of is check every keystroke for ',' but how would I save the 1st string to a temp string, and then save only the 2nd string to a temp string?

0

Share this post

Link to post

Share on other sites

It sounds like you're working with a textbox here. Wait until the user has type in the complete string, then parse it. Its properly more trouble than its worth doing it on the fly. Anyway, when you have the complete string, search for , and make substrings: Look up the IndexOf and Substring methods of the String class.

If this doesn't help, you must provide a little more info about the problem.

0

Share this post

Link to post

Share on other sites

Original post by MCeltic33I need to parse a string so that when the user types "Lastname, Firstname" it catches the lastname into one string and the first name into another string - and allows for the user to use the backspace and work properly.

the only thing i could think of is check every keystroke for ',' but how would I save the 1st string to a temp string, and then save only the 2nd string to a temp string?

Wait for the TextBox to lose focus (check the LostFocus event), then check. Parse by using String.Split on the comma; if there's more than one comma, you can raise a validation error. The previous poster's suggestions of using IndexOf() and Substring() are a little too clunky for this particular case.

hope that helps,

0

Share this post

Link to post

Share on other sites

Original post by HesterumpeIt sounds like you're working with a textbox here. Wait until the user has type in the complete string, then parse it. Its properly more trouble than its worth doing it on the fly. Anyway, when you have the complete string, search for , and make substrings: Look up the IndexOf and Substring methods of the String class.

If this doesn't help, you must provide a little more info about the problem.

The problem with this is I need to update the display as the user is typing...

0

Share this post

Link to post

Share on other sites

derive a control from textbox, leave the standard Text property functioning as is, but add an additional property that is an array of strings, something like Tokens. Every change, reparse the string and set the Tokens property accordingly, and fire appropriate events.

0

Share this post

Link to post

Share on other sites

To find out in "real time" what the user is typing, you just need to handle the "KeyPress" event. Add an event handler to handle this event, and this will get called every time the user has pressed a key. Then, when the user has typed something, you can get the tokens using one of the techniques described in this thread.