<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>
このデザインは以下のようになります
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();
}
}
出力は以下のようになります
[バックスペース]ボタンをクリックすると、テキストボックスが削除され、カーソルがテキストボックスにフォーカスされます。問題は、[バックスペース]ボタンをクリックして押したままにすると、テキストボックスの値が繰り返し削除されないことです。コメントの場合、GetInstance.Focus(); 上記のコードから、値は繰り返し削除されますが、テキストボックスのテキスト値を繰り返し削除すると、カーソルがフォーカスされなくなります。
しかし、GetInstance.Focus ();の前にevent(txt_remove_GotFocus)を削除するかどうかはわかります。、Backspaceボタンをクリックして押したままにすると、テキストボックスの値が繰り返し削除されます。その後、rbtn_remove_LostMouseCaptureenventに新しいイベントハンドラーが追加されます。
最後に、私は以下のシナリオを達成したいと思います。
例:テキストボックスに値を入力し、システムキーボードからバックスペースキーをクリックして押したままにすると、差額が請求されます。
上記のシナリオで他のアイデアがあれば、私と共有してください。