locked
How to stop th cursor moves out of the TextBox in WPF RRS feed

  • Question

  • Hi,

    We have a TextBox. The cursor moves out of the boundary of textbox even though we set max length.

    <Style TargetType="{x:Type ctr:CustomTextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ctr:CustomTextBox}">
                        <ControlTemplate.Resources>
                            <Storyboard x:Key="enterGotFocus" >
                                <DoubleAnimation Duration="0:0:0.2" To=".33"
    			  	Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                            </Storyboard>
                            <Storyboard x:Key="exitGotFocus" >
                                <DoubleAnimation Duration="0:0:0.4"
    			  	Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                            </Storyboard>
                            <Storyboard x:Key="enterHasText" >
                                <DoubleAnimation Duration="0:0:0.2" From=".33" To="0"
    			  	Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                            </Storyboard>
                            <Storyboard x:Key="exitHasText" >
                                <DoubleAnimation Duration="0:0:0.4"
    			  	Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Message"/>
                            </Storyboard>
    
                        </ControlTemplate.Resources>
                        <Grid ClipToBounds="True">
    
                            <Border x:Name="Border_Decorator"
                                    BorderThickness="1,1,0,0"
                                    BorderBrush="#FFD9DBDE"
                                    HorizontalAlignment="Stretch"
                                    VerticalAlignment="Stretch">
                            </Border>
                            <Border Name="Bd"
    							    Background="{TemplateBinding Background}"
    							    BorderBrush="{TemplateBinding BorderBrush}"
    							    BorderThickness="{TemplateBinding BorderThickness}">
                                <Grid>
                                    <Decorator x:Name="PART_ContentHost" VerticalAlignment="Top" Margin="3"/>
                                    <TextBlock Text="{TemplateBinding TextBoxLabel}" x:Name="Message"
    								    Foreground="Black" IsHitTestVisible="False"
    								     Opacity="0.67" HorizontalAlignment="Left" VerticalAlignment="Top"
    								    Margin="6,0,0,0"/>
                                </Grid>
                            </Border>
                        </Grid>
    
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="HasText" Value="False"/>
                                    <Condition Property="IsFocused" Value="True"/>
                                </MultiTrigger.Conditions>
                                <MultiTrigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource enterGotFocus}"/>
                                </MultiTrigger.EnterActions>
                                <MultiTrigger.ExitActions>
                                    <BeginStoryboard Storyboard="{StaticResource exitGotFocus}"/>
                                </MultiTrigger.ExitActions>
                            </MultiTrigger>
                            <Trigger Property="Focusable" Value="False">
                                <Setter Property="Visibility" Value="Collapsed" TargetName="Border_Decorator" />
                            </Trigger>
                            <Trigger Property="HasText" Value="True">
                                <Trigger.EnterActions>
                                    <BeginStoryboard Storyboard="{StaticResource enterHasText}"/>
                                </Trigger.EnterActions>
                                <Trigger.ExitActions>
                                    <BeginStoryboard Storyboard="{StaticResource exitHasText}"/>
                                </Trigger.ExitActions>
                            </Trigger>
    
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter TargetName="Bd" Property="Background" Value="LightGray"/>
                                <Setter Property="Foreground" Value="Gray"/>
                            </Trigger>
    
                                 <Trigger Property="IsMandatory" Value="True">
                                <Setter TargetName="Bd" Property="Background" Value="LightYellow"/>
                            </Trigger>
                            <Trigger Property="Width" Value="Auto">
                                <Setter Property="MinWidth" Value="100"/>                          
                            </Trigger>
                            <Trigger Property="Height" Value="Auto">
                                <Setter Property="MinHeight" Value="20"/>
                                
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    Styles what we applied for the textbox is mentioned above.

    Please post some sample code
    Wednesday, March 3, 2010 9:05 AM

Answers

  • Hi Rain,

    To prevent the mouse cursor from moving out of a TextBox, you can handle the LostKeyboardFocus event and set the keyboard focus to that TextBox in the event handler. The following is a sample.

     private void textbox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
     {
         this.textbox.Focus();
     }


    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Wednesday, March 10, 2010 3:25 AM
    Sunday, March 7, 2010 10:32 AM

All replies

  • Hi Rain drops,

    Could you please provide a simplified and ready-to-run example to demonstrate the issue you are encountering? So that we can have a better understanding about your issue.

    Thanks,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Friday, March 5, 2010 10:37 AM
  • In addition to what Linda suggested,
    it would be helpful if you elaborate on and clarify your problem...

    First of all, you are not using the System.Windows.TextBox class, but rather your own custom-built text box implementation.
    Second, this is just a style for your text box, which probably does not have anything to do with your problem. It would be more helpful to show the code for your custom text box rather than the style that is to be applied to it.

    Thank you.
    Friday, March 5, 2010 12:54 PM
  • Hi Rain,

    To prevent the mouse cursor from moving out of a TextBox, you can handle the LostKeyboardFocus event and set the keyboard focus to that TextBox in the event handler. The following is a sample.

     private void textbox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
     {
         this.textbox.Focus();
     }


    Sincerely,
    Linda Liu


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Linda Liu Wednesday, March 10, 2010 3:25 AM
    Sunday, March 7, 2010 10:32 AM