Set Cursor to beginning MaskedTextBox

6 posts, 1 answers
  1. Lookoum
    Lookoum avatar
    7 posts
    Member since:
    Nov 2011

    Posted 26 Mar 2012 Link to this post

    Hi everybody!

    I'm looking something which allow me to set the cursor at the beginning of my MaskedTextBox.

    I have a mask like this : '00 00 00 00 00'
    When I enter into my field, my cursor isn't at begin.    '00 0|0 00 00 00' instead of    '|00 00 00 00 00'

    I tried this code :

    private void maskedTextBox1_Enter(object sender, EventArgs e)
            {
                if ((maskedTextBox1.Text.Trim()).Length == 0)
                {
                    maskedTextBox1.Focus();
                    SendKeys.Send("{HOME}");
                    MessageBox.Show(maskedTextBox1.Focused.ToString());
                }
            }

    But it doesn't work.

    I tried the same code on MouseUp Action but I need to select the control.
    If I do a MouseUp inside MaskedTextBox nothing happend, but if I do a MouseUp on the border of my MaskedTextBox, it works. Why?

    What should I do in order to set my cursor in the first position?

    I tried maskedTextBox1.SelectionStart = 0; but doesn't work.

    Thanks

    Lookoum
  2. Lookoum
    Lookoum avatar
    7 posts
    Member since:
    Nov 2011

    Posted 26 Mar 2012 Link to this post

    Hi again,

    I found something in order to fix my problem.

    I think it's not the best way but it works.

    private void mskTelephone_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((mskTelephone.Text.Trim()).Length == 0)
        {
            mskTelephone.Focus();
            SendKeys.Send("{HOME}");
            mskTelephone.Text = e.KeyChar.ToString();
        }
    }
     
    private void mskTelephone_KeyUp(object sender, KeyEventArgs e)
    {
        if ((mskTelephone.Text.Trim()).Length == 1)
        {
            mskTelephone.SelectionStart = 1;
        }
    }

    My cursor has the right position but only when I start writing in my control.

    How can I have the right position when my MaskedTextBox got the focus?

    Thanks

    Lookoum

  3. Answer
    Peter
    Admin
    Peter avatar
    1148 posts

    Posted 29 Mar 2012 Link to this post

    Hi Lookoum,

    Thank you for this question.

    You should handle TextBoxItem MouseUp event and RadMaskedEditBox Enter event and set the RadMaskEditBox SelectionStart property to 0.

    Please refer to the attached project.

    I hope this helps.

    Greetings,
    Peter
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  4. Lookoum
    Lookoum avatar
    7 posts
    Member since:
    Nov 2011

    Posted 29 Mar 2012 Link to this post

    Hi Peter,

    Thanks a lot!

    I was looking for the TextBoxElement in the MaskedTextBox in order to catch the mouse_up event but didn't find it.

    this.mskTelephone.MaskedEditBoxElement.TextBoxItem.MouseUp += new MouseEventHandler(TextBoxItem_MouseUp);

    Have a nice day!

    Best regards

    Lookoum
  5. Dante
    Dante avatar
    3 posts
    Member since:
    May 2017

    Posted 21 Jun in reply to Peter Link to this post

    Hi Peter,

    I have the same problem but I'n not using Visual Studio I'm using only Telereik Test studio in a test with a WPF aplication, I tried with your solution and the solution of lookoum usig a step code and it doesn't work, If you could help me I would thank you.

    Greetings.

  6. Dess
    Admin
    Dess avatar
    2057 posts

    Posted 23 Jun Link to this post

    Hello Dante, 

    Thank you for writing.  

    I would like to note that this forum is related to the Telerik UI for WinForms suite. However, it seems that your question is related to WPF. Feel free to post in the relevant forum: http://www.telerik.com/forums

    Thank you for your understanding.

    Regards,
    Dess
    Progress Telerik
    Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Back to Top