Solved

visual C#, maskedTextbox, moving cursor to begining on focus

Posted on 2009-05-05
6
1,610 Views
Last Modified: 2013-12-17
Hi Friends,

I am developing windows application using VS .net 3.5 using C#, right now in my application i have used Maskedtextboxes. Now i want to know how can i move my cursor to beginning of MaskedTextbox on focus.

Moreover as I want to do the same thing for all maskedtextboxes how can i do it with single function wired with on focus or any other Event .

I have already tried  <controlname>.SelectionStart = 0, but its not working.


Thanks,
0
Comment
Question by:ArunVashist
[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
6 Comments
 
LVL 8

Expert Comment

by:RyanAndres
ID: 24307846
use Select method.
Select(int start, int length)

ie.
maskedTextBox1.Select(0,0);
0
 
LVL 12

Accepted Solution

by:
wht1986 earned 150 total points
ID: 24307932
MaskedTextBox handle their own selection so to move the cursor to the beginning try

        private void maskedTextBox1_Enter(object sender, EventArgs e)
        {
            MaskedTextBox tb = (MaskedTextBox)sender;
            this.BeginInvoke((MethodInvoker)delegate() { tb.Select(0, 0); });
        }
0
 

Author Comment

by:ArunVashist
ID: 24308056
Thanks wht1986,

it works..  it would be kind if you just explain me why we need to do it this way... actually i could'nt understand the second line of function.

thanks in advance.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:wht1986
ID: 24308172
The masked textbox actually is doing its own select on focus because of the mask, so when we say move to the beginning, the built in select says move to the end executes after ours.

So what we need to do is basically queue up (delay the execution of our select) by using the BeginInvoke method.
0
 
LVL 12

Expert Comment

by:wht1986
ID: 24308188
I only know this because I had to solve the same thing this week at work and it took me countless google hours to find the answer, but I'm glad I could help
0
 
LVL 3

Expert Comment

by:RobbP
ID: 24308202
Hi,

What you could do is the attached.
If you want to make it reusable then create a generic event with the same signature as that of events you require fired and wire the other events up within this at runtime ie mosedown, enter etc.
namespace Scratchpad
{
    public partial class Form1 : Form
    {
        private bool f = false;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        void maskedTextBox1_GotFocus(object sender, EventArgs e)
        {
            maskedTextBox1.SelectionStart = 0;
        }
 
        private void maskedTextBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.f)
            { this.maskedTextBox1.SelectionStart = 0; }
            this.f = false;
        }
 
        private void maskedTextBox1_Enter(object sender, EventArgs e)
        {
            this.f = true;
            this.maskedTextBox1.SelectionStart = 0;
        }
 
        private void maskedTextBox1_MouseMove(object sender, MouseEventArgs e)
        {
            this.f = false;
        }
    }
}

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

696 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