Reputation: 1
I want enter number in textbox, an my textbox convert automatically it in comma(,) format. I have tried to do this, but it works wrong. Help me? Like this 1,20(I JUST ENTER 120);
private bool IsNumeric(int Val)
{
return ((Val >= 48 && Val <= 57) || (Val == 8) || (Val == 46));
}
String str;
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
int KeyCode = e.KeyValue;
if (!IsNumeric(KeyCode))
{
if (KeyCode == 13)
{
e.Handled = true;
vendas();
str = null;
}
e.Handled = true;
return;
}
else
{
e.Handled = true;
}
if (((KeyCode == 8) || (KeyCode == 46)) && (str.Length > 0))
{
str = str.Substring(0, str.Length - 1);
}
else if (!((KeyCode == 8) || (KeyCode == 46)))
{
str = str + Convert.ToChar(KeyCode);
}
if (str.Length == 0)
{
textBox1.Text = "";
}
if (str.Length == 1)
{
textBox1.Text = "0,0" + str;
}
else if (str.Length == 2)
{
textBox1.Text = "0," + str;
}
else if ((str.Length > 2) && (str.Length != 6) && (str.Length != 9) && (str.Length != 12))
{
textBox1.Text = str.Substring(0, str.Length - 2) + "," + str.Substring(str.Length - 2);
textBox1.Text = textBox1.Text;
}
else if ((str.Length > 6) && (str.Length != 8) && (str.Length != 10) && (str.Length != 12))
{
textBox1.Text = str.Substring(0, str.Length - 3) + "," + str.Substring(str.Length - 1);
textBox1.Text = textBox1.Text;
}
}
It shows me 10,01 instead 0,01?
Upvotes: 0
Views: 21534
Reputation: 1319
I was originally going to suggest MaskedTextBox, but MTB is designed for left-to-right style formatting of a fixed (or known in advanced, at least) length string, which makes it not so suitable for currency.
First, I'd recommend you avoid doing anything using keycodes or the like, and stick to something a bit more straightforward by just validating and editing the text when it changes:
void tb_TextChanged(object sender, EventArgs e)
{
//Remove previous formatting, or the decimal check will fail
string value = tb.Text.Replace(",", "").Replace("$", "");
decimal ul;
//Check we are indeed handling a number
if (decimal.TryParse(value, out ul))
{
//Unsub the event so we don't enter a loop
tb.TextChanged -= tb_TextChanged;
//Format the text as currency
tb.Text = string.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:C2}", ul);
tb.TextChanged += tb_TextChanged;
}
}
The main part is string.Format(CultureInfo.CreateSpecificCulture("en-US"), "{0:C2}", ul);
the en-US ensures it'll always show $ and decmials as '.'s. The {0:C2}
formats the string as a number (0
) of currency (:C
) to 2 decimal spaces (2
).
This doesn't prevent the user from entering text, but you can keep the previous text (eg '$23.00') and if the user enters something that isn't a number, decimal.TryParse will fail, at which point you can revert the text back to what it was before the user changed it (just by inserting an else block near the end of this event handler).
I'd recommend setting the TB text to '$0.00' or something initially, or the cursor will jump when it formats the first change. It also has some selection issues when commas are adding, which you could get around by storing the selection position just before formatting and changing it afterward, or doing something more complex, it's just an example.
Upvotes: 1
Reputation: 71591
What you want is a MaskedTextBox.
Simply set of mask of "$999,999,990.00"
and any input the user enters must be digits, and there must be at least 3, but the entry can be any number up to the hundreds of millions (if you need billions and trillions, just add more 9s and commas). As the user enters these digits, the formatting will adjust based on the mask. Here's the kicker; MaskedTextBox respects culture information, so if you specify the French culture, the commas become spaces, the decimal becomes a comma, and the dollar sign becomes the Euro symbol.
Upvotes: 1
Reputation: 8818
Well, at first glance, I notice that nothing about your code whatsoever makes any sense.
First and foremost, learn coding standards for indentation. This code is extremely hard to read. I'm tempted to flag this question as offensive for having had to look at it.
Next, this line:
if (!IsNumeric(KeyCode){
Says: if the keycode is NOT numeric do the following stuff, where "the following stuff" is a whole bunch of numeric operations on a keycode which is presumed to be numeric.
Next, str
is not defined anywhere in your method. Maybe it's defined globally, but that would just be silly. Rather, you should just get the current value of it programmatically.
And finally: you don't need to reinvent the wheel. There are tons of tools out there that will do this kind of thing for you. In fact, I'm fairly sure win forms has a native control that'll do this. It might even be an attribute of textbox, I don't remember.
Upvotes: -2