【问题标题】:How to focs cursor in textbox when click the RepeatButton in wpf?单击wpf中的RepeatButton时如何将光标聚焦在文本框中?
【发布时间】:2012-08-02 17:07:58
【问题描述】:
 <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;                   
                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();
    }


}

输出如下所示

当我单击退格按钮时,文本框将被删除,光标将聚焦在文本框中。问题是,当我单击并按住退格按钮时,文本框值会反复删除,但光标未显示。

对于例如:在文本框中输入值,然后单击并按住系统键盘上的退格键,您将获得差价。

【问题讨论】:

    标签: c# wpf textbox setfocus repeatbutton


    【解决方案1】:

    使用下面的代码它会很好地帮助你。为什么因为前一天我遇到了同样的问题。

    <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" Focusable="False"  Content="Backspace" Delay="400" Interval="100" Margin="415,123,0,0" RenderTransformOrigin="0.667,0.854" Click="rbtn_remove_Click" LostMouseCapture="rbtn_remove_LostMouseCapture" HorizontalAlignment="Left" Height="41" VerticalAlignment="Top" Width="66"/>        
    </Grid>
    

    在上面的代码中我只添加了一个属性Focusable="False"

     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;                   
                    GetInstance.Focus();
                    GetInstance.CaretIndex = _CareIndex - 1;                   
    
                }
            }
        }
        void txt_remove_GotFocus(object sender, RoutedEventArgs e)
        {
            GetTextbox = (Control)sender;  
        }
        private void rbtn_remove_LostMouseCapture(object sender, MouseEventArgs e)
        {
            GetInstance.Focus();
        }
    

    在上面的代码中,我刚刚添加了单个代码 GetInstance.Focus();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-29
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多