Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a masked textbox which is used to enter the fax number in specified format. i have a problem on caret start position in that. when i click on last position in the box it remains in the same position, but the caret should be in the starting position (|+1- - - , like this).

C#
<msk:MaskedTextBox x:Name="txt_Fax" Mask="+1-000-000-0000" PromptChar=" "
     Text="{Binding Path=Fax}"  MouseDown="txt_Fax_MouseDown"  />

  // (Codebehind)
   private void txt_Fax_MouseDown(object sender, MouseButtonEventArgs e)
    {
        txt_Fax.SelectionStart = txt_Fax.MaskedTextProvider.LastAssignedPosition + 1;
        txt_Fax.SelectionLength = 0;
    }


in which event i have to write this code?
Posted
Updated 22-Jan-15 0:21am
v3
Comments
George Swan 22-Jan-15 5:13am    
I think you may be getting confused between cursor and caret. The flashing vertical bar is the caret. Its position is determined by the caretIndex property.
Ranjith Mameras 22-Jan-15 6:22am    
yup!! i got confused.. can you tell me how to set that caret position at start of the masked textbox?
George Swan 22-Jan-15 9:26am    
I think that your problem arises because part of the normal functionality of the textBox is to place the caret at the position of the cursor when the left mouse button is clicked, as this facilitates editing. By changing this functionality you will lose operational consistency between related controls which may both confuse and frustrate users. My only suggestion is that, to achieve the effects you want, you may have to replace the default template for the control with your own version. This is not a trivial task. Best wishes.
Ranjith Mameras 22-Jan-15 23:46pm    
Thank you George for ur replies :)
George Swan 23-Jan-15 0:53am    
You are very welcome - I'm sorry that I couldn't be of more help.

1 solution

try another event like GotFocus or MouseUp.
When you "activate" a control, there are a bunch of events happening, if you set the caret too soon, the value will be overwritten by an event that comes later in the chain.


Hope this helps.
 
Share this answer
 
Comments
Ranjith Mameras 23-Jan-15 9:53am    
is there any other way to do this ??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900