Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does WPF textbox not support triple-click to select all text

Tags:

c#

wpf

textbox

is there a reason why a WPF textbox does not support triple-click behaviour like most modern UIs these days do?

By "triple click", I mean: double clicking a line of text in a textbox selects a single word, while triple clicking selects the whole line.

Is there an attribute that can be applied to textbox, or another workaround? Is there any chance Microsoft will implement it as default behaviour for a WPF textbox?

like image 384
Erik Avatar asked Feb 17 '17 11:02

Erik


People also ask

What is the use of textbox in WPF?

The TextBox control provides support for basic text input in WPF applications. Gets or sets the style used by this element when it is rendered. Represents a control that provides a scroll bar that has a sliding Thumb whose position corresponds to a value.

What is the difference between double click and triple click?

Triple click feels more like a shortcut in the Shortcuts/Intents/Actions triad. When I double click somewhere, my intent may be to select that word. If I triple click, my intent may be to select the line. Or it may be to select the paragraph.

How to select all text in a textfield in flutter?

On native platforms and browsers using a triple click (with a mouse) on text in a textfield one selects all text in the textfield. Flutter doesn't do this, but rather deselects the selected word. Flutter should support this to match native platforms and browsers.

How to retrieve selected text from a textbox using clickevent?

In this example, a button with an associated Clickevent handler is used to retrieve the text selection. When the user clicks the button, the OnClickmethod copies any selected text in the textbox into a string.


2 Answers

As already answered, you can implement that manually. But you don't want to do that for each textbox in your application. So you can implement attached property and set it in style, like this:

public static class TextBoxBehavior {
    public static readonly DependencyProperty TripleClickSelectAllProperty = DependencyProperty.RegisterAttached(
        "TripleClickSelectAll", typeof(bool), typeof(TextBoxBehavior), new PropertyMetadata(false, OnPropertyChanged));

    private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
        var tb = d as TextBox;
        if (tb != null) {
            var enable = (bool)e.NewValue;
            if (enable) {
                tb.PreviewMouseLeftButtonDown += OnTextBoxMouseDown;
            }
            else {
                tb.PreviewMouseLeftButtonDown -= OnTextBoxMouseDown;
            }
        }
    }

    private static void OnTextBoxMouseDown(object sender, MouseButtonEventArgs e) {
        if (e.ClickCount == 3)
        {
            ((TextBox)sender).SelectAll();
        }
    }

    public static void SetTripleClickSelectAll(DependencyObject element, bool value)
    {
        element.SetValue(TripleClickSelectAllProperty, value);
    }

    public static bool GetTripleClickSelectAll(DependencyObject element) {
        return (bool) element.GetValue(TripleClickSelectAllProperty);
    }
}

Then create style somewhere (for example in application resources in App.xaml):

<Application.Resources>
    <Style TargetType="TextBox">
        <Setter Property="local:TextBoxBehavior.TripleClickSelectAll"
                Value="True" />
    </Style>
</Application.Resources>

Now all your textboxes will automatically have this triple-click behavior.

like image 168
Evk Avatar answered Oct 19 '22 18:10

Evk


You can try it by using PreviewMouseDown-Event

private void MyTextBoxPreviewMouseDown(object sender, MouseButtonEventArgs e){

  if (e.ClickCount == 3) {
    MyTexBox.SelectAll();
  }
}
like image 7
Jehof Avatar answered Oct 19 '22 19:10

Jehof