From 6ebca5dab4959c1b2c4f02b91b4f643762cf6f11 Mon Sep 17 00:00:00 2001 From: Pavel Shakhov Date: Sat, 12 Jul 2014 16:59:44 +0400 Subject: [PATCH] =?UTF-8?q?Fix=20#6=20-=20=D0=A2=D0=B0=D0=B9=D0=BC=D0=B5?= =?UTF-8?q?=D1=80=20=D0=B7=D0=B0=D0=BA=D0=BE=D0=BD=D1=87=D0=B8=D0=BB=D1=81?= =?UTF-8?q?=D1=8F.=20=D0=9A=D0=BB=D0=B8=D0=BA=D0=B0=D0=B5=D1=88=D1=8C=20?= =?UTF-8?q?=D0=BF=D0=BE=20=D0=BF=D0=BE=D0=BB=D1=8E=20(=D1=87=D1=82=D0=BE?= =?UTF-8?q?=D0=B1=D1=8B=20=D0=BA=D1=83=D1=80=D1=81=D0=BE=D1=80=20=D0=BF?= =?UTF-8?q?=D0=BE=D0=BF=D0=B0=D0=BB=20=D0=BD=D0=B0=20=D1=86=D0=B8=D1=84?= =?UTF-8?q?=D1=80=D1=83)=20-=20=D1=81=D0=B1=D1=80=D0=B0=D1=81=D1=8B=D0=B2?= =?UTF-8?q?=D0=B0=D0=B5=D1=82=D1=81=D1=8F=20=D0=B2=D1=8B=D0=B4=D0=B5=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Orzeszek Timer/ClickSelectTextBox.cs | 54 ++++++++++++++++++++++++++++ Orzeszek Timer/MainWindow.xaml | 8 ++--- Orzeszek Timer/Orzeszek Timer.csproj | 1 + 3 files changed, 59 insertions(+), 4 deletions(-) create mode 100644 Orzeszek Timer/ClickSelectTextBox.cs diff --git a/Orzeszek Timer/ClickSelectTextBox.cs b/Orzeszek Timer/ClickSelectTextBox.cs new file mode 100644 index 0000000..fb725dd --- /dev/null +++ b/Orzeszek Timer/ClickSelectTextBox.cs @@ -0,0 +1,54 @@ +namespace OrzeszekTimer +{ + using System.Windows; + using System.Windows.Controls; + using System.Windows.Input; + using System.Windows.Media; + + /// + /// We have it so the first click selects all, and another click goes to cursor + /// + /// + /// http://stackoverflow.com/a/661224/136559 + /// + public class ClickSelectTextBox : TextBox + { + public ClickSelectTextBox() + { + this.AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true); + this.AddHandler(GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true); + this.AddHandler(MouseDoubleClickEvent, new RoutedEventHandler(SelectAllText), true); + } + + private static void SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e) + { + // Find the TextBox + DependencyObject parent = e.OriginalSource as UIElement; + while (parent != null && !(parent is TextBox)) + { + parent = VisualTreeHelper.GetParent(parent); + } + + if (parent != null) + { + var textBox = (TextBox)parent; + if (!textBox.IsKeyboardFocusWithin) + { + // If the text box is not yet focussed, give it the focus and + // stop further processing of this click event. + textBox.Focus(); + e.Handled = true; + } + } + } + + private static void SelectAllText(object sender, RoutedEventArgs e) + { + var textBox = e.OriginalSource as TextBox; + if (textBox != null) + { + textBox.SelectAll(); + } + } + } +} diff --git a/Orzeszek Timer/MainWindow.xaml b/Orzeszek Timer/MainWindow.xaml index 757b607..4df611d 100644 --- a/Orzeszek Timer/MainWindow.xaml +++ b/Orzeszek Timer/MainWindow.xaml @@ -21,8 +21,8 @@ along with this program. If not, see . + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:OrzeszekTimer="clr-namespace:OrzeszekTimer" + Title="Orzeszek Timer" Height="150" Width="300" Loaded="Window_Loaded" SizeChanged="Window_SizeChanged" StateChanged="Window_StateChanged" Closing="Window_Closing" Closed="Window_Closed" Activated="Window_Activated" Deactivated="Window_Deactivated" MouseUp="Window_MouseUp" KeyUp="Window_KeyUp" KeyDown="Window_KeyDown"> @@ -88,7 +88,7 @@ along with this program. If not, see . - + @@ -110,7 +110,7 @@ along with this program. If not, see . - + \ No newline at end of file diff --git a/Orzeszek Timer/Orzeszek Timer.csproj b/Orzeszek Timer/Orzeszek Timer.csproj index 9d0454b..2b21799 100644 --- a/Orzeszek Timer/Orzeszek Timer.csproj +++ b/Orzeszek Timer/Orzeszek Timer.csproj @@ -57,6 +57,7 @@ MSBuild:Compile Designer + True True