Solved

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

Posted on 2009-05-05
6
1,562 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

759 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

15 Experts available now in Live!

Get 1:1 Help Now