Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the habilitiy to block the use of the scientific notation 'e'. #2423

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions MahApps.Metro/Controls/NumericUpDown.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ private static void InterceptManualEnterChangedCallback(DependencyObject depende
typeof(NumericUpDown),
new FrameworkPropertyMetadata(true, OnHasDecimalsChanged));

public static readonly DependencyProperty HasScientificNotationProperty = DependencyProperty.Register(
"HasScientificNotation",
typeof(bool),
typeof(NumericUpDown),
new PropertyMetadata(true));

private static readonly Regex RegexStringFormatHexadecimal = new Regex(@"^(?<complexHEX>.*{\d:X\d+}.*)?(?<simpleHEX>X\d+)?$", RegexOptions.Compiled);

private const double DefaultInterval = 1d;
Expand Down Expand Up @@ -463,6 +469,18 @@ public bool HasDecimals
set { SetValue(HasDecimalsProperty, value); }
}

/// <summary>
/// Indicates if the NumericUpDown should accept the scientific notation 'e' or not.
/// </summary>
[Bindable(true)]
[Category("Common")]
[DefaultValue(true)]
public bool HasScientificNotation
{
get { return (bool)GetValue(HasScientificNotationProperty); }
set { SetValue(HasScientificNotationProperty, value); }
}

/// <summary>
/// Called when this element or any below gets focus.
/// </summary>
Expand Down Expand Up @@ -704,13 +722,14 @@ protected void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
else if (textBox.SelectionStart > 0)
{
string elementBeforeCaret = textBox.Text.ElementAt(textBox.SelectionStart - 1).ToString(equivalentCulture);
if (elementBeforeCaret.Equals(ScientificNotationChar, StrComp))
if (elementBeforeCaret.Equals(ScientificNotationChar, StrComp) && HasScientificNotation)
{
e.Handled = false;
}
}
}
else if (text.Equals(ScientificNotationChar, StrComp) &&
else if (HasScientificNotation &&
text.Equals(ScientificNotationChar, StrComp) &&
textBox.SelectionStart > 0 &&
!textBox.Text.Any(i => i.ToString(equivalentCulture).Equals(ScientificNotationChar, StrComp)))
{
Expand Down
11 changes: 11 additions & 0 deletions samples/MetroDemo/ExampleViews/TextExamples.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,16 @@
Margin="1"
Content="HasDecimals"
IsChecked="True" />
<CheckBox x:Name="HasScientificNotationCheckBox"
Margin="1"
Content="HasScientificNotation"
IsChecked="True" />
</UniformGrid>

<Label Content="Min=&quot;0&quot;, Max=&quot;10&quot;, TextAlignment=&quot;Left&quot;" />
<Controls:NumericUpDown x:Name="Test"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
IsEnabled="False"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
Maximum="10"
Expand All @@ -255,6 +260,7 @@
<Controls:NumericUpDown Controls:TextBoxHelper.ClearTextButton="True"
ButtonsAlignment="Left"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
Interval="5"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
IsTabStop="False"
Expand All @@ -263,13 +269,15 @@
<Controls:NumericUpDown Margin="{StaticResource ControlMargin}"
Controls:TextBoxHelper.Watermark="No Speedup when long pressed"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
Speedup="false" />
<Controls:NumericUpDown Margin="{StaticResource ControlMargin}"
Controls:TextBoxHelper.UseFloatingWatermark="True"
Controls:TextBoxHelper.Watermark="Speedup when pressed after 1000ms"
Delay="1000"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
Speedup="true" />

Expand All @@ -278,6 +286,7 @@
ContentStringFormat="StringFormat, Real Value = {0}" />
<Controls:NumericUpDown x:Name="test"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
Maximum="100"
StringFormat="pcs. {0:N2} pcs."
Expand Down Expand Up @@ -309,6 +318,7 @@
<Controls:NumericUpDown x:Name="StringFormatNumUpDown"
Margin="{StaticResource ControlMargin}"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
Interval=".1"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
StringFormat="0,000.00"
Expand All @@ -318,6 +328,7 @@
<Controls:NumericUpDown Controls:TextBoxHelper.SelectAllOnFocus="True"
ButtonsAlignment="Left"
HasDecimals="{Binding ElementName=HasDecimalsCheckBox, Path=IsChecked, Mode=TwoWay}"
HasScientificNotation="{Binding ElementName=HasScientificNotationCheckBox, Path=IsChecked, Mode=TwoWay}"
Interval="5"
IsReadOnly="{Binding ElementName=ReadOnlyCheck, Path=IsChecked, Mode=TwoWay}"
IsTabStop="False"
Expand Down