Skip to content
This repository has been archived by the owner on Aug 26, 2018. It is now read-only.

Commit

Permalink
Fix #6 - Таймер закончился. Кликаешь по полю (чтобы курсор попал на ц…
Browse files Browse the repository at this point in the history
…ифру) - сбрасывается выделение
  • Loading branch information
pongo committed Jul 12, 2014
1 parent 77c4d3a commit 6ebca5d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
54 changes: 54 additions & 0 deletions Orzeszek Timer/ClickSelectTextBox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
namespace OrzeszekTimer
{
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;

/// <summary>
/// We have it so the first click selects all, and another click goes to cursor
/// </summary>
/// <remarks>
/// http://stackoverflow.com/a/661224/136559
/// </remarks>
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();
}
}
}
}
8 changes: 4 additions & 4 deletions Orzeszek Timer/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

<Window x:Class="OrzeszekTimer.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
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">
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">
<Window.Resources>
<ControlTemplate x:Key="ValidTextBoxTemplate" TargetType="{x:Type TextBox}">
<Border x:Name="Border" Background="#FFFFFF" BorderBrush="#DDDDDD" BorderThickness="1" Padding="1" SnapsToDevicePixels="True">
Expand Down Expand Up @@ -88,7 +88,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</ControlTemplate>
</ProgressBar.Template>
</ProgressBar>
<TextBox x:Name="MainTextBox" FontSize="16" FontWeight="Bold" Margin="10" Template="{StaticResource ValidTextBoxTemplate}" TextWrapping="Wrap" Validation.ErrorTemplate="{x:Null}" GotFocus="MainTextBox_GotFocus" LostFocus="MainTextBox_LostFocus" KeyUp="MainTextBox_KeyUp" PreviewMouseLeftButtonDown="MainTextBox_PreviewMouseLeftButtonDown">
<OrzeszekTimer:ClickSelectTextBox x:Name="MainTextBox" FontSize="16" FontWeight="Bold" Margin="10" Template="{StaticResource ValidTextBoxTemplate}" TextWrapping="Wrap" Validation.ErrorTemplate="{x:Null}" GotFocus="MainTextBox_GotFocus" LostFocus="MainTextBox_LostFocus" KeyUp="MainTextBox_KeyUp" PreviewMouseLeftButtonDown="MainTextBox_PreviewMouseLeftButtonDown">
<TextBox.ContextMenu>
<ContextMenu x:Name="MainContextMenu">
<MenuItem x:Name="NoNotificationMenuItem" Header="No notification" IsCheckable="True" Click="SoundMenuItem_Click"/>
Expand All @@ -110,7 +110,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
</MenuItem>
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</OrzeszekTimer:ClickSelectTextBox>
<Button x:Name="StopNotificationButton" FontSize="16" FontWeight="Bold" Margin="10" Template="{StaticResource FillButton}" Visibility="Collapsed" Click="StopNotificationButton_Click">Stop</Button>
</Grid>
</Window>
1 change: 1 addition & 0 deletions Orzeszek Timer/Orzeszek Timer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="ClickSelectTextBox.cs" />
<Compile Include="Resource.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
Expand Down

0 comments on commit 6ebca5d

Please sign in to comment.