Solved

HowTo: Select text

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
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…

726 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