fifamaniac04
fifamaniac04

Reputation: 2383

Xceed MaskedTextBox does not start at beginning when focused

I am using Xceed MaskedTextBox;

<xctk:MaskedTextBox Mask="000-000-0000" x:Name="PhoneTextBox">
     <xctk:MaskedTextBox.Style>
       <xctk:MaskedTextBox.Style>
           <Style.Triggers>
              <MultiTrigger>
                 <MultiTrigger.Conditions>
                   <Condition Property="Text" Value="___-___-____"/>
                 </MultiTrigger.conditions>
              </MultiTrigger>
            </Style.Triggers>
       </xctk:MaskedTextBox.Style>

When ever I click in the text box to enter a number, it always positions my cursor where i clicked instead of at the beginning of the textbox. This causes the user to start typing a number and quickly running out of room since if they click near the end, they can enter fewer numbers.

Q: Is there a way to click anywhere in the box and have the cursor start position at the front?

Upvotes: 1

Views: 575

Answers (2)

Smiles
Smiles

Reputation: 71

picking up on Gary Wright answer. how to position on the last character in the masktextbox

int startPos = MaskTextbox1.MaskedTextProvider.FindUnassignedEditPositionFrom(MaskTextbox1.MaskedTextProvider.LastAssignedPosition + 1, true);
MaskTextbox1.Focus();
MaskTextbox1.Select(startPos, 0);
e.Handled = true;

Upvotes: 0

Gary Wright
Gary Wright

Reputation: 2469

Something like this should work for you; in the markup:

<xctk:MaskedTextBox x:Name="MyMaskedTextBox"  Mask="(000) 000-000" PreviewMouseDown="MyMaskedTextBox_PreviewMouseDown"></xctk:MaskedTextBox>

And the event handler:

private void MyMaskedTextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    MyMaskedTextBox.Focus();
    MyMaskedTextBox.Select(1, 0);
    e.Handled = true;            
}

On the call to Select, pass the index of where you want the cursor to be, depending on your mask.

Upvotes: 1

Related Questions