Click here to Skip to main content
16,023,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sorry for posting something similar to my older post. But the answer for the old one suggested to use regex. However I want to learn how to do it using TryParse. So here is the code:

private void InputBox_TextChanged(object sender, EventArgs e)
        {
            if (InputBox.Text.StartsWith("π") == false)//Clear the box if the message does not start with π
            {
                InputBox.Text = "";
            }
            if ((InputBox.Text.StartsWith("π") == true) && (InputBox.Text.EndsWith("}") == true))// only process if the message starts with π and ends with }
            {
                string Message = InputBox.Text;
                InputBox.Text = "";// Clear the box when done. 
                if (Message.StartsWith("πImageBox_Coordinates{") == true) ///Coordinates
                {
                    Message = Message.Substring(22);
                    Message = Message.TrimEnd('}');      
                    string[] MessageSplit = Message.Split(new char[] { ',' });           
                    for (int i = 0; i < MessageSplit.Count(); i++)
                    {
                        buttcorrs[i] = Convert.ToInt16(MessageSplit[i]);
                        //////////////////////
                    }



What I am trying is I am getting a string through inputbox by pressing buttons and it sends me hardcoded string such as {123,344,456,456} however when i press morethan one button the string gets screwed and sends weird characters. When I get the string the way i want to I trim it and send it to an array so each number becomes a index for buttcorrs array above example it becomes: buttcorrs[0] = 123, buttcorrs[1] = 344 and so on. So when i get weird characters the program crashes. Some how I need to check the string before I trim it and make sure it only contains numbers. I couldn't make regex work and want to try, tryParse. but cant figure that out too.

So if someone can teach me how to use tryParse I will appreciate it.

Thank you,
Posted
Comments
William Winner 25-May-10 11:53am    
will your numbers always contain 3 digits? or could you get {34,2343,233,1)?

I ask because any of the Intxx.TryParse will only work if the comma represents a thousands separator and since that's not actually what it represents, it's not really the best solution.
TolgaCiftci 25-May-10 14:24pm    
Thanks for taking your time and trying to help I used regular expression and fixed the problem. but to answer your question it does contain 1 or more digits so it can be {34,2343,233,1)

Try using this version of TryParse. This has a NumberStyles and a IFormatProvider.
 
Share this answer
 
1. MSDN[^] tells how to use it.

2. You do not need the "== false" part in the following code. The method itself returns a boolean values so you do not need to compare it again. See about "!" operator. Same goes with other similar if conditions as well.

TolgaCiftci wrote:
InputBox.Text.StartsWith("π") == false
 
Share this answer
 
Comments
William Winner 25-May-10 11:55am    
I tried explaining that to him when he asked about RegEx. He didn't seem to get it then. Besides the fact that two of us gave him a solution using RegEx that he apparently couldn't make work.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900