Solved

C# string manipulation

Posted on 2016-08-22
2
47 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now