<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
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
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.
Why don't you use RepeatButton class?
Citation from MSDN:
Here's MVVM-way code sample:
1) Sample view model:
3) Window markup:
3) Window code-behind:
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:
2) Window code-behind: