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

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

问题描述

所以,假设我有一个带有几个文本框的WPF表单,如果你选中了文本框,并且它已经包含了一些东西,我想选择那个文本框中的所有文本,这样就可以删除那个文本。如果你用鼠标点击该框,这可能意味着你想在某个地方改变一个字母,所以在这种情况下不要突出显示所有的字母。似乎很容易,但一个很好的解决方案,迄今为止逃避我。这是我到目前为止,非常接近工作,但并不完美。

 < ; Style x:Key =TextBoxStyleTargetType =TextBox> 
< EventSetter Event =GotKeyboardFocusHandler =EventSetter_OnHandler/>
< / style>

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





$ b因此当箱子获得键盘焦点时,它会选择全部,框完美地选择所有的文本。但是,如果鼠标单击此方法也会被调用,这也会突出显示文本,但是单击会将光标放在鼠标点击之后。所以在功能上它是完美的,但它仍然困扰我,当鼠标点击时,它会闪烁选择所有内容。任何更好的方式来做到这一点,或在我的事件中进行某种检查,知道我从鼠标单击而不是选项卡获得键盘焦点?

解决方案



 <$ c  

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

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

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



$ b $ p
$ b

在这一点上你仍然可以看到一些选择被点击清除,但这是只是因为以前的选择只有在不重点的情况下才会被隐藏。您可以始终清除 LostKeyboardFocus 中的选项以防止发生这种情况(例如((TextBox)sender).Select(0,0))。


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();

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();
}

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天全站免登陆