Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Set cursor position in TextBox

Posted on 2012-08-16
9
Medium Priority
?
1,172 Views
Last Modified: 2012-08-17
I have a MaskedTextBox with mask "\\ABC0000000".
When the TextBox is displayed, the cursor is at the A. I would like it to be before the first 0.
How do I set the cursor position?
0
Comment
Question by:allelopath
  • 4
  • 4
9 Comments
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 2000 total points
ID: 38302643
Via code, you'd set SelectionStart() to 3, and SelectionLength to 0 (zero).
0
 
LVL 1

Author Comment

by:allelopath
ID: 38302663
I did this:
this.myTextBox.SelectionLength = 0;
this.myTextBox.SelectionStart = 3;

Open in new window

but it seems to have no effect.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38302745
Try focusing the TextBox before you fiddle with the selection points:


this.myTextBox.Focus();
this.myTextBox.SelectionLength = 0;
this.myTextBox.SelectionStart = 3;

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38302828
If that doesn't help, where and when you do have that code?
0
 
LVL 1

Author Comment

by:allelopath
ID: 38305363
No difference using Focus().

I'm playing with the location of those lines of code. The class is a subclass of UserControl. The first location was the constructor. Now I've moved it to OnLoad(), but still no joy.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 38305386
It's not something that can be done once, such as the Contructor() or OnLoad().  You'd have to do it in the GotFocus() event of your TextBox, for example...
0
 
LVL 1

Author Comment

by:allelopath
ID: 38305428
I see your point. In that case, I would like to do something like this:
this.myTextBox.OnGotFocus += new System.EventHandler(this.myTextBox_OnGotFocus);

Open in new window

but OnGotFocus is not an option.
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 38305461
Use GotFocus():

            this.textBox1.GotFocus += new EventHandler(textBox1_GotFocus);

        void textBox1_GotFocus(object sender, EventArgs e)
        {
           // ... previous code here ...   
        }

Open in new window

0
 
LVL 1

Author Comment

by:allelopath
ID: 38305486
That's it:
private void myTextBox_GotFocus(object sender, EventArgs e)
{
        this.txtCrrNumber.SelectionStart = 3;
        this.txtCrrNumber.SelectionLength = 0;
}

Open in new window

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Screencast - Getting to Know the Pipeline
Suggested Courses

564 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