Solved

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

Posted on 2009-05-05
6
1,579 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
DevOps Toolchain Recommendations

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

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now