Reputation: 109
For my program, employees info are inputted - like ID, name, surname and yearly salary. I made the ID and salary box numericupdown but I want to make the salary box a textbox that only takes numbers.
however when i try to change from numericupdown to text box an error occurs saying "cannot implicitly convert "string" to "decimal". I also have another button that finds the lowest salary etc but what is the problem? I want to create the exception to make the textbox only take numbers but it won't let me :/
Upvotes: 2
Views: 2190
Reputation: 71563
You can create a "masked" TextBox that will only allow characters that would match a Regex when added to the existing text.
The short of it:
First, develop a Regex that is your "edit mask". For numbers, it can be as simple as ^\d*$
(integer of any length) or as complex as ^(\d{1,3}(,?\d\d\d)*(.\d{1,})?)?$
(number with optional commas and optional decimal part). Assign this to a public property of a new control deriving from TextBox.
Then, override OnKeyPress in your derived textbox to look something like this:
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!Regex.Match(new StringBuilder(Text).Append(e.KeyChar).ToString(), MaskPattern))
{
e.Handled = true;
}
else
base.OnKeyPress(e);
}
Much like Robert Harvey's answer, this method will "swallow" any key press that, when appended to the current text value of the textbox, doesn't match the Regex. You can use this MaskedTextBox not only for number entries, but for textboxes that require alpha-only values, alphanumeric-only (no symbols or whitespace), valid local or network paths, IP addresses, etc etc.
Upvotes: 1
Reputation: 236218
Here is one hack I found. You can hide arrows of NumericUpDown control this way:
private void RemoveArrows(NumericUpDown numericUpDown)
{
Control updown = numericUpDown.Controls[0];
updown.Left += updown.Width;
updown.Visible = false;
}
Just call this method for your NumericUpDown controls (e.g. on Form_Load event handler):
private void Form2_Load(object sender, EventArgs e)
{
RemoveArrows(idNumericUpDown);
RemoveArrows(salaryNumericUpDown);
}
Other ways - create numeric text box, or use parsing and validation:
ErrorProvider
component from ToolBox to your form.CausesValidation
property to true
for textbox (by default it is true).If decimal value could not be parsed from text in textbox, error sign will be displayed near textbox with message "Only numeric values allowed".
private void NumericTextBox_Validating(object sender, CancelEventArgs e)
{
TextBox textBox = sender as TextBox;
decimal value;
if (Decimal.TryParse(textBox.Text, out value))
{
errorProvider1.SetError(textBox, "");
return;
}
e.Cancel = true;
errorProvider1.SetError(textBox, "Only numeric values allowed");
}
Advise - use NumericUpDown controls for entering numbers, because it says to user "See this arrows? I'm here for entering only numbers!". And textbox says nothing about text format it accepts.
Upvotes: 1
Reputation: 5600
this is what i always do for my textbox.
Function (allows all numeric values and the backspace key):
private bool NumericOnly(char e)
{
return (e > (char)47 & e < (char)58) | e == (char)8;
}
and onkeypress event of textbox :
if (!NumericOnly(e.KeyChar)) e.Handled = true;
Upvotes: 1
Reputation: 4296
however when i try to change from numericupdown to text box an error occurs saying "cannot implicitly convert "string" to "decimal".
That looks to me like a casting error.
employee.EmployeeId = (int)idNumericUpDown.Value;
Try this instead:
employee.EmployeeId = decimal.Parse(idNumericUpDown.Value);
And also look into TryParse
, which is cleaner.
Upvotes: 4
Reputation: 180788
How to Create a Numeric Textbox in C#
http://msdn.microsoft.com/en-us/library/ms229644(v=vs.90).aspx
In its simplest form:
protected override void OnKeyPress(KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar))
{
e.Handled = true;
}
}
This code just swallows any keystroke that is not a number. The MSDN link explains how you can handle things like decimal points, etc.
Upvotes: 9