Solved

C# string manipulation

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting C# Objects into JSON file 7 64
C# Error - Add Failed 12 78
Definitions and default visual studio colors 5 58
Get id from json Data with NewtonSoft 3 31
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

932 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