简体   繁体   中英

issue in RepeatButton in wpf

<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>

code In 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;
    }

}

With above code i can get the result below.

的RepeatButton

If i click remove button, the textbox value clear.But If i click and hold remove button,It is not remove the textbox value repeatedly .

everything is good enough to do the work you want to do. but the focus() method call on Getinstance. is making the diffrance.

just remove .

GetInstance.Focus();

will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM