[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

HowTo: Select text

Posted on 2002-07-18
1
Medium Priority
?
197 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 200 total points
ID: 7163935
The text box must have focus in order for SelectionStart and SelectionLength to work.
0

Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
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…

650 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