Solved

HowTo: Select text

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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