Solved

C# string manipulation

Posted on 2016-08-22
2
65 Views
Last Modified: 2016-08-22
Lets say I have a string, i.e.
"firstname1 lastname1; firstname2 lastname2; firstname3 lastname3; firstname4 lastname4; "
in a TextBox, how can I by clicking on any firstname or lastname get both selected together with semicolon?
0
Comment
Question by:ybt
2 Comments
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 500 total points
ID: 41765891
Something like this would break it apart and do the selection.
        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                //get the position of our click.
                int position = textBox1.SelectionStart;
                //figure out where the semi colon before this selection is.
                string formerText = textBox1.Text.Substring(0, position);
                int startPosition = formerText.LastIndexOf(';') + 1;
                //figure out where the semi colon after the start of this name block is.
                string nameText = textBox1.Text.Substring(startPosition);
                int semiColonPosition = nameText.IndexOf(';');
                //select the items between the two semicolons.
                textBox1.SelectionStart = startPosition;
                textBox1.SelectionLength = semiColonPosition;
            }
        }

Open in new window

0
 

Author Closing Comment

by:ybt
ID: 41765969
Thank you, it's working, I just had to check for empty string if an user clicks outside of a text in the TextBox.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question