0
<Grid x:Name="LayoutRoot">
    <TextBox x:Name="txt_remove" Height="46" Margin="234,119,225,0" TextWrapping="Wrap" VerticalAlignment="Top" GotFocus="txt_remove_GotFocus"/>
    <RepeatButton x:Name="rbtn_remove" Content="Remove" Delay="500" Interval="100"  Margin="283.667,183,282.333,222" RenderTransformOrigin="0.667,0.854" Click="rbtn_remove_Click" />                  
</Grid>

c#中的代码

public partial class Repeate : Window
{
    Control GetTextbox;

    public Repeate()
    {
        this.InitializeComponent(); 

    }

    private void rbtn_remove_Click(object sender, RoutedEventArgs e)
    {

         TextBox 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;
            }
        }
    }

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

}

使用上面的代码,我可以得到下面的结果。

重复按钮

如果我单击删除按钮,文本框值会清除。但是如果我单击并按住删除按钮,它不会重复删除文本框值

4

1 回答 1

2

一切都足够好,可以做你想做的工作。但是在 Getinstance 上调用 focus() 方法。正在制造差异。

只需删除 .

GetInstance.Focus();

将工作。

于 2012-08-01T08:57:35.333 回答