[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

C# string manipulation

Posted on 2016-08-22
2
Medium Priority
?
82 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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