Solved

HowTo: Select text

Posted on 2002-07-18
1
191 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 18
Video Player 11 23
Hey!!!! 1 20
List<PaisEntity> - show some 1 24
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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

786 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