Skip to content

Commit 98c1580

Browse files
committed
Add validation to the NumberPicker to prevent values from being set lower/higher than a specific value.
1 parent ae8723f commit 98c1580

File tree

5 files changed

+48
-6
lines changed

5 files changed

+48
-6
lines changed

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@
218218
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
219219
<WarningLevel>4</WarningLevel>
220220
</PropertyGroup>
221+
<PropertyGroup>
222+
<StartupObject />
223+
</PropertyGroup>
221224
<ItemGroup>
222225
<Reference Include="Antlr4.Runtime.net45">
223226
<HintPath>..\packages\Antlr4.Runtime.4.3.0\lib\net45\Antlr4.Runtime.net45.dll</HintPath>

RetailCoder.VBE/UI/Controls/NumberPicker.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
Name="Root">
99
<Grid>
1010
<StackPanel Orientation="Horizontal">
11-
<TextBox Margin="5,5,0,5" Height="20" Width="50" Text="{Binding ElementName=Root, Path=NumValue, Mode=TwoWay, StringFormat=\{0:D\}, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" VerticalContentAlignment="Center" />
11+
<TextBox Margin="5,5,0,5" Height="20" Width="50" Text="{Binding ElementName=Root, Path=NumValue, Mode=TwoWay, StringFormat=\{0:D\}, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" VerticalAlignment="Top" VerticalContentAlignment="Center" />
1212
<StackPanel Margin="0,5" >
1313
<Button Height="10" Width="20" Click="cmdUp_Click">
1414
<TextBlock Text="˄" FontSize="10" Margin="0,-4,0,0"/>

RetailCoder.VBE/UI/Controls/NumberPicker.xaml.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using System.Windows;
1+
using System.ComponentModel;
2+
using System.Windows;
23

34
// credit to http://stackoverflow.com/a/2752538
45
namespace Rubberduck.UI.Controls
56
{
67
/// <summary>
78
/// Interaction logic for NumberPicker.xaml
89
/// </summary>
9-
public partial class NumberPicker
10+
public partial class NumberPicker : IDataErrorInfo
1011
{
1112
public static readonly DependencyProperty NumValueProperty =
1213
DependencyProperty.Register("NumValue", typeof(int), typeof(NumberPicker), new UIPropertyMetadata(null));
@@ -24,6 +25,20 @@ public int NumValue
2425
}
2526
}
2627

28+
private int _minNumber = int.MinValue;
29+
public int MinNumber
30+
{
31+
get { return _minNumber; }
32+
set { _minNumber = value; }
33+
}
34+
35+
private int _maxNumber = int.MaxValue;
36+
public int MaxNumber
37+
{
38+
get { return _maxNumber; }
39+
set { _maxNumber = value; }
40+
}
41+
2742
public NumberPicker()
2843
{
2944
InitializeComponent();
@@ -38,5 +53,25 @@ private void cmdDown_Click(object sender, RoutedEventArgs e)
3853
{
3954
NumValue--;
4055
}
56+
57+
public string this[string columnName]
58+
{
59+
get
60+
{
61+
if (columnName != "NumValue")
62+
{
63+
return string.Empty;
64+
}
65+
66+
if (NumValue < MinNumber || NumValue > MaxNumber)
67+
{
68+
return "Invalid Selection";
69+
}
70+
71+
return string.Empty;
72+
}
73+
}
74+
75+
public string Error { get; private set; }
4176
}
4277
}

RetailCoder.VBE/UI/Settings/GeneralSettings.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
<controls:NumberPicker Margin="0,-6,0,0"
9292
IsEnabled="{Binding ElementName=AutoSaveButton, Path=IsChecked}"
9393
NumValue="{Binding AutoSavePeriod, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
94+
MinNumber="1"
9495
HorizontalAlignment="Left" />
9596
</StackPanel>
9697

RetailCoder.VBE/UI/Settings/IndenterSettings.xaml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@
7878
<controls:NumberPicker Margin="0,-6,0,0"
7979
IsEnabled="{Binding ElementName=AlignDimColumn, Path=IsChecked}"
8080
NumValue="{Binding AlignDimColumn, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
81-
HorizontalAlignment="Left" />
81+
HorizontalAlignment="Left"
82+
MinNumber="0"/>
8283
</StackPanel>
8384
<Label Content="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=IndenterSettings_SpecialOptionsLabel}"
8485
FontWeight="SemiBold" />
@@ -123,7 +124,8 @@
123124
Margin="5,0,0,5"
124125
HorizontalAlignment="Left" />
125126
<controls:NumberPicker Margin="0,-6,0,0"
126-
NumValue="{Binding IndentSpaces, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
127+
NumValue="{Binding IndentSpaces, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
128+
MinNumber="0"/>
127129
</StackPanel>
128130
<Label Content="{Resx ResxName=Rubberduck.UI.RubberduckUI, Key=IndenterSettings_EnableOptionsLabel}"
129131
FontWeight="SemiBold" />
@@ -146,7 +148,8 @@
146148
<controls:NumberPicker Margin="0,-4,0,0"
147149
NumValue="{Binding EndOfLineCommentColumnSpaceAlignment, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
148150
Visibility="{Binding EndOfLineCommentStyle, Converter={StaticResource EndOfLineCommentStyleToVisibility}}"
149-
HorizontalAlignment="Left" />
151+
HorizontalAlignment="Left"
152+
MinNumber="0" />
150153
</StackPanel>
151154
</StackPanel>
152155
</ScrollViewer>

0 commit comments

Comments
 (0)