Solved

HowTo: Select text

Posted on 2002-07-18
1
190 Views
Last Modified: 2008-02-26
I'm a C# newbie trying to convert a MFC application to C#. I have a multiline read-only text box and I want a substring of the textbox text to be selected.

In the constructor, this works:

InitializeComponent();
myTextBox.Text = "0123456789";
myTextBox.SelectionStart = 2;
myTextBox.SelectionLength = 3;

My question concerns causing selection highlighting during a "click handler". For instance, I have a button that should cause a substring within the myTextBox to be selected:

private void buttonClicked(object sender, System.EventArgs e)
{
  myTextBox.SelectionStart = 2;
  myTextBox.SelectionLength = 3;
  tbContext.Invalidate();  // Doesn't help
}

Seems like the above ought to work, but nothing happens. My impression is I need a statement (or statements) that does something like "invalidate" so the myTextBox will be repainted and the selection will take effect. What am I doing wrong or leaving out?

TIA
0
Comment
Question by:newton-allan
1 Comment
 
LVL 3

Accepted Solution

by:
CesarGon earned 50 total points
ID: 7163935
The text box must have focus in order for SelectionStart and SelectionLength to work.
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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

19 Experts available now in Live!

Get 1:1 Help Now