Question

 <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; 
                // I want o remove the Gotfocus envet here.  
                GetInstance.Focus(); //If i comment this line cursor will not focus on textbox
                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 in not removing repeatedly. If comment the GetInstance.Focus(); from above code the values are removing repeatedly but the cursor in not focusing when repeatedly removing the text value on textbox.

But i have an idea if remove the event(txt_remove_GotFocus) before GetInstance.Focus();, the textbox values are removing repeatedly when click and hold the Backspace button.After then will add new event handler in rbtn_remove_LostMouseCapture envent.

Finaly i want to achieve the below scenario.

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.

If you any other idea means for above scenario, Please share with me.

Was it helpful?

Solution

Why don't you use RepeatButton class?

Citation from MSDN:

Represents a control that raises its Click event repeatedly from the time it is pressed until it is released.

Here's MVVM-way code sample:

1) Sample view model:

public class ViewModel : ViewModelBase
{
    public ViewModel()
    {
        this.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";
        this.backspaceCommand = new RelayCommand(
            () => Text = Text.Substring(0, Text.Length - 1), 
            () => !String.IsNullOrEmpty(Text));
    }

    public String Text
    {
        get { return text; }
        set
        {
            if (text != value)
            {
                text = value;
                OnPropertyChanged("Text");
            }
        }
    }
    private String text;

    public RelayCommand BackspaceCommand
    {
        get { return backspaceCommand; }
    }
    private readonly RelayCommand backspaceCommand;
}

3) Window markup:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="114" Width="404">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Column="0" Margin="5" x:Name="tbText" Text="{Binding Text, UpdateSourceTrigger=PropertyChanged}"/>
        <RepeatButton Grid.Column="1" Margin="5" Content="Backspace" Command="{Binding BackspaceCommand}"/>
    </Grid>
</Window>

3) Window code-behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += (sender, args) => 
        { 
            tbText.Focus(); 
            tbText.CaretIndex = tbText.Text.Length; 
        };
        DataContext = new ViewModel();
    }
}

Here's ugly code-behind-way sample:

UPDATE. This sample has been updated to show caret, when button is pressed. The first, we should disable focusing on the button. The second, we should fix caret position after the text in TextBox was changed.

1) Windows markup:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="114" Width="404">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBox Grid.Column="0" Margin="5" x:Name="tbText" Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit."/>
        <RepeatButton Grid.Column="1" Margin="5" Focusable="False" Content="Backspace" Click="RepeatButton_Click"/>
    </Grid>
</Window>

2) Window code-behind:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        Loaded += (sender, args) => 
        { 
            tbText.Focus(); 
            tbText.CaretIndex = tbText.Text.Length; 
        };
    }

    private void RepeatButton_Click(object sender, RoutedEventArgs e)
    {
        if (!string.IsNullOrEmpty(tbText.Text))
        {
            tbText.Text = tbText.Text.Substring(0, tbText.Text.Length - 1);
            tbText.CaretIndex = tbText.Text.Length;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top