选项卡上的文本框 SelectAll 但不是鼠标单击 [英] Textbox SelectAll on tab but not mouse click

查看:13
本文介绍了选项卡上的文本框 SelectAll 但不是鼠标单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有多个文本框的 WPF 表单,如果您选择到文本框并且其中已经有一些内容,我想选择该框中的所有文本,以便键入将删除该文本.如果您在该框上单击鼠标,则可能意味着您想在某处更改字母,因此在这种情况下不要突出显示所有字母.似乎很容易,但到目前为止我还没有找到一个好的解决方案.到目前为止,这是我非常接近工作的东西,但不是很完美.

So lets say I have a WPF form with several text boxes, if you tab to the text box and it already has something in it, I want to select all the text in that box so typing will erase that text. If you mouse click on the box, it probably means you want to change a letter somewhere, so do not highlight all in this case. Seems easy enough, but a good solution as so far eluded me. Here's what I have so far that is very close to working, but not quite perfect.

<Style x:Key="TextBoxStyle" TargetType="TextBox">
    <EventSetter Event="GotKeyboardFocus" Handler="EventSetter_OnHandler" />
</Style>

private void EventSetter_OnHandler(object sender, RoutedEventArgs e)
{
    TextBox txt = sender as TextBox;
    if (txt != null) txt.SelectAll();
}

因此,当框获得键盘焦点时,它会选择所有文本,因此在文本框上跳动可以完美地选择所有文本.但是,如果鼠标单击此方法也会被调用,这也会突出显示文本,但单击会将光标置于鼠标单击之后的位置.所以在功能上它是完美的,但它仍然困扰着我,当鼠标点击时它会闪烁选择所有内容.有没有更好的方法来做到这一点,或者在我的事件中进行某种检查以知道我通过鼠标单击而不是选项卡获得了键盘焦点?

So when the box gets keyboard focus it selects all, so tabbing to the text box selects all the text perfectly. However if the mouse clicks this method gets called as well, which also highlights the text, but the click then puts the cursor where the mouse clicked after. So functionally it's perfect, but it still bothers me that it flickers selecting everything when the mouse clicks. Any better way to do this, or put some kind of check in my event to know that I gained keyboard focus from a mouse click and not a tab?

推荐答案

遗憾的是,到目前为止还没有看到任何干净的解决方案,您可以做的一件事就是检查鼠标状态:

Have not seen any clean solution so far sadly, one thing you could do is just check the mouse state:

var tb = (TextBox)sender;
if (Mouse.LeftButton != MouseButtonState.Pressed)
    tb.SelectAll();

但实际上有一个更好的方法,当焦点转移到按键时,您可以改为检查键盘.我建议为 GotKeyboardFocus 处理程序使用正确的签名来获取适当的事件参数:

But there actually is a better way, as the focus shifts on key down you can check the keyboard instead. I would recommend using the proper signature for the GotKeyboardFocus handler to get the appropriate event-args:

private void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
    if (e.KeyboardDevice.IsKeyDown(Key.Tab))
        ((TextBox)sender).SelectAll();
}

此时您可能仍会看到某些选择在单击时被清除,但这只是因为之前的选择仅在未聚焦时才会隐藏.您可以随时清除 LostKeyboardFocus 中的选择以防止发生这种情况(例如 ((TextBox)sender).Select(0, 0)).

At this point you may still see some selection getting cleared upon click but that is just because the previous selection only gets hidden if unfocused. You can always clear the selection in LostKeyboardFocus to prevent that (e.g. ((TextBox)sender).Select(0, 0)).

这篇关于选项卡上的文本框 SelectAll 但不是鼠标单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆