From 19c5d23a1ba96f95030b79b0cdae33e573be95ae Mon Sep 17 00:00:00 2001 From: Brian Lima Date: Thu, 19 Oct 2017 16:33:31 -0200 Subject: [PATCH 1/2] Checking if the user is typing valid values Using regex and preview input to check if the user is typing valid values. --- ColorTools/ColorControlPanel.xaml | 10 +++++----- ColorTools/ColorControlPanel.xaml.cs | 13 +++++++++++++ TEST_ColorPanel/TEST_ColorPanel.csproj | 2 +- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/ColorTools/ColorControlPanel.xaml b/ColorTools/ColorControlPanel.xaml index 00f7d97..11c2d4a 100644 --- a/ColorTools/ColorControlPanel.xaml +++ b/ColorTools/ColorControlPanel.xaml @@ -373,7 +373,7 @@ + HorizontalContentAlignment="Center" FontFamily="Cambria Math" VerticalContentAlignment="Bottom" Margin="0" VerticalAlignment="Center" Padding="0,1,0,0" FontSize="16" Height="25" Width="38" BorderBrush="{x:Null}" PreviewTextInput="TextBoxPreviewTextInput"/> @@ -387,7 +387,7 @@ + DockPanel.Dock="Right" VerticalAlignment="Bottom" Background="{Binding Background, ElementName=txtRvalue, Mode=TwoWay}" Foreground="{Binding Foreground, ElementName=txtRvalue}" FontSize="14" FontFamily="Lucida Console" BorderBrush="{x:Null}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="95" Height="27" Padding="0" PreviewTextInput="ColorCode_PreviewTextInput"/> diff --git a/ColorTools/ColorControlPanel.xaml.cs b/ColorTools/ColorControlPanel.xaml.cs index 7fd6bdf..586f161 100644 --- a/ColorTools/ColorControlPanel.xaml.cs +++ b/ColorTools/ColorControlPanel.xaml.cs @@ -15,6 +15,7 @@ using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; +using System.Text.RegularExpressions; namespace ColorTools { @@ -834,6 +835,18 @@ private static void IniColorChanged(DependencyObject d, DependencyPropertyChange new FrameworkPropertyMetadata(new SolidColorBrush(Color.FromRgb(85, 85, 85)), new PropertyChangedCallback(SelectedColorChanged))); + private void TextBoxPreviewTextInput(object sender, TextCompositionEventArgs e) + { + Regex regex = new Regex("[^0-9]+"); + e.Handled = regex.IsMatch(e.Text); + } + + private void ColorCode_PreviewTextInput(object sender, TextCompositionEventArgs e) + { + Regex regex = new Regex("^([A-Fa-f0-9]{2}){8,9}$"); + e.Handled = regex.IsMatch(e.Text); + } + public SolidColorBrush SelectedColorBrush { get { return (SolidColorBrush)GetValue(SelectedColorBrushProperty); } diff --git a/TEST_ColorPanel/TEST_ColorPanel.csproj b/TEST_ColorPanel/TEST_ColorPanel.csproj index 94a7e79..500ee06 100644 --- a/TEST_ColorPanel/TEST_ColorPanel.csproj +++ b/TEST_ColorPanel/TEST_ColorPanel.csproj @@ -37,7 +37,7 @@ False - ..\ColorTools\bin\Release\ColorTools.dll + ..\ColorTools\bin\Debug\ColorTools.dll From 2e1593e851b00d0eb5eea18890eb25ec96e7a28f Mon Sep 17 00:00:00 2001 From: Brian Lima Date: Fri, 20 Oct 2017 08:14:34 -0200 Subject: [PATCH 2/2] Minor design changes --- ColorTools/ColorControlPanel.xaml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/ColorTools/ColorControlPanel.xaml b/ColorTools/ColorControlPanel.xaml index 11c2d4a..1911b62 100644 --- a/ColorTools/ColorControlPanel.xaml +++ b/ColorTools/ColorControlPanel.xaml @@ -5,7 +5,7 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:ColorTools" mc:Ignorable="d" - d:DesignHeight="160" d:DesignWidth="400"> + d:DesignHeight="160" d:DesignWidth="400" FontFamily="Segoe UI"> @@ -349,7 +349,7 @@ - + @@ -369,11 +369,11 @@ - - - - + DockPanel.Dock="Right" VerticalAlignment="Bottom" Background="{Binding Background, ElementName=txtRvalue, Mode=TwoWay}" Foreground="{Binding Foreground, ElementName=txtRvalue}" FontSize="14" BorderBrush="{x:Null}" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Width="95" Height="27" Padding="0" PreviewTextInput="ColorCode_PreviewTextInput"/> @@ -443,5 +443,5 @@ - +