I am doing an Financial Winforms application and am having some trouble with the controls.
My customer needs to insert decimal values all over the place (Prices, Discounts etc) and I'd like to avoid some of the repeating validation.
So I immediately tried the MaskedTextBox that would fit my needs (with a Mask like "€ 00000.00"), if it weren't for the focus and the length of the mask.
I can't predict how big the numbers are my customer is going to enter into the app.
I also can't expect him to start everything with 00 to get to the comma. Everything should be keyboard-friendly.
Am I missing something or is there simply no way (beyond writing a custom control) to achieve this with the standard Windows Forms controls?
This two overriden methods did it for me (disclaimer: this code is not in production yet. You may need to modify)
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!char.IsNumber(e.KeyChar) & (Keys)e.KeyChar != Keys.Back
& e.KeyChar != '.')
{
e.Handled = true;
}
base.OnKeyPress(e);
}
private string currentText;
protected override void OnTextChanged(EventArgs e)
{
if (this.Text.Length > 0)
{
float result;
bool isNumeric = float.TryParse(this.Text, out result);
if (isNumeric)
{
currentText = this.Text;
}
else
{
this.Text = currentText;
this.Select(this.Text.Length, 0);
}
}
base.OnTextChanged(e);
}
You will need a custom control. Just trap the Validating event on the control and check if the string input can be parsed as a decimal.
I don't think you need a custom control, just write a decimal validating method for the validating event and use that for all the places you need to validate. Don't forget to include the NumberFormatInfo, it will deal with commas and numebr signs.
MSDN: User Input Validation in Windows Forms
You only need to let numbers and decimal symbols through, and avoid a double decimal symbol. As an extra, this automatically adds a 0 before a starting decimal number.
public class DecimalBox : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == ',')
{
e.KeyChar = '.';
}
if (!char.IsNumber(e.KeyChar) && (Keys)e.KeyChar != Keys.Back && e.KeyChar != '.')
{
e.Handled = true;
}
if(e.KeyChar == '.' )
{
if (this.Text.Length == 0)
{
this.Text = "0.";
this.SelectionStart = 2;
e.Handled = true;
}
else if (this.Text.Contains("."))
{
e.Handled = true;
}
}
base.OnKeyPress(e);
}
}
Another approach is to block what you don't want, and format when your done with it.
class DecimalTextBox : TextBox
{
// Handle multiple decimals
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (e.KeyChar == '.')
if (this.Text.Contains('.'))
e.Handled = true;
base.OnKeyPress(e);
}
// Block non digits
// I scrub characters here instead of handling in OnKeyPress so I can support keyboard events (ctrl + c/v/a)
protected override void OnTextChanged(EventArgs e)
{
this.Text = System.Text.RegularExpressions.Regex.Replace(this.Text, "[^.0-9]", "");
base.OnTextChanged(e);
}
// Apply our format when we're done
protected override void OnLostFocus(EventArgs e)
{
if (!String.IsNullOrEmpty(this.Text))
this.Text = string.Format("{0:N}", Convert.ToDouble(this.Text));
base.OnLostFocus(e);
}
}