Solved

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

Posted on 2009-05-05
6
1,586 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
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

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…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

773 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