Set Cursor to beginning MaskedTextBox

4 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. UI for WinForms is Visual Studio 2017 Ready
  4. 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 >>
  5. 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
Back to Top