Solved

C# string manipulation

Posted on 2016-08-22
2
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 13

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

724 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