Solved

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

Posted on 2009-05-05
6
1,595 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Convert datetime to time string 10 28
How to read text with RegEx... 2 25
Input parameteres to DragOver 2 19
SSRS Deployment problem 5 64
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…
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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

840 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