Search code examples
c#wpftextboxsetfocusrepeatbutton

How to focs cursor in textbox when click the RepeatButton in wpf?


 <Grid x:Name="BackSpaceButton">       
    <TextBox x:Name="txt_remove" Height="46" Margin="234,119,225,0" TextWrapping="Wrap" VerticalAlignment="Top" GotFocus="txt_remove_GotFocus" TabIndex="2"/>        
    <RepeatButton x:Name="rbtn_remove" Content="Backspace" Delay="400" Interval="200" Margin="415,124,0,0" RenderTransformOrigin="0.667,0.854" Click="rbtn_remove_Click" LostMouseCapture="rbtn_remove_LostMouseCapture" HorizontalAlignment="Left" Height="41" VerticalAlignment="Top" Width="66" TabIndex="2" />        
</Grid>

This design will be like below

enter image description here

public partial class Repeate : Window
{
    Control GetTextbox;
    TextBox GetInstance;
    public Repeate()
    {
        this.InitializeComponent();
    }

    private void rbtn_remove_Click(object sender, RoutedEventArgs e)
    {

        GetInstance = GetTextbox as TextBox;
        if (GetTextbox != null)
        {

            string _CurrentValue = GetInstance.Text;
            var _CareIndex = GetInstance.CaretIndex;

            if (_CareIndex > 0)
            {
                string _Backspace = _CurrentValue.Remove(_CareIndex - 1, 1);
                GetInstance.Text = _Backspace;                   
                GetInstance.CaretIndex = _CareIndex - 1;
            }
        }
    }

    private void txt_remove_GotFocus(object sender, RoutedEventArgs e)
    {
        GetTextbox = (Control)sender;
    }

    private void rbtn_remove_LostMouseCapture(object sender, MouseEventArgs e)
    {
        GetInstance.Focus();
    }


}

Out put will be like below

enter image description here

When i click the Backspace button textbox will remove and the cursor will be focusing in textbox.What is the issue is, when i click and hold the Backspace button the textbox value removing repeatedly but the cursor in not showing.

For Ex: Enter the value in textbox and then click and hold the backspace key from your system keyboard then you will fee the difference.


Solution

  • Use the below code it will help you well.why because some day before i faced the same issue.

    <Grid x:Name="BackSpaceButton">
        <TextBox x:Name="txt_remove" Height="46" Margin="234,119,225,0" TextWrapping="Wrap" VerticalAlignment="Top" GotFocus="txt_remove_GotFocus" TabIndex="2"/>        
        <RepeatButton x:Name="rbtn_remove" Focusable="False"  Content="Backspace" Delay="400" Interval="100" Margin="415,123,0,0" RenderTransformOrigin="0.667,0.854" Click="rbtn_remove_Click" LostMouseCapture="rbtn_remove_LostMouseCapture" HorizontalAlignment="Left" Height="41" VerticalAlignment="Top" Width="66"/>        
    </Grid>
    

    in above code i just add one property Focusable="False"

     private void rbtn_remove_Click(object sender, RoutedEventArgs e)
        {
            GetInstance = GetTextbox as TextBox;
            if (GetTextbox != null)
            {
    
                string _CurrentValue = GetInstance.Text;
                var _CareIndex = GetInstance.CaretIndex;
    
                if (_CareIndex > 0)
                {
                    string _Backspace = _CurrentValue.Remove(_CareIndex - 1, 1);
                    GetInstance.Text = _Backspace;                   
                    GetInstance.Focus();
                    GetInstance.CaretIndex = _CareIndex - 1;                   
    
                }
            }
        }
        void txt_remove_GotFocus(object sender, RoutedEventArgs e)
        {
            GetTextbox = (Control)sender;  
        }
        private void rbtn_remove_LostMouseCapture(object sender, MouseEventArgs e)
        {
            GetInstance.Focus();
        }
    

    in above code I've just added single code GetInstance.Focus();