Reputation: 323
Windows Forms C# - I would like to make a textbox that automatically changes each time a user types or deletes one key from the textbox. I developed part of code.
//This will convert value from textbox to currency format when focus leave textbox
private void txtValormetrocubico_Leave(object sender, EventArgs e)
{
decimal cubic = Convert.ToDecimal(txtValormetrocubico.Text);
txtValormetrocubico.Text = string.Format("{0:c}", Convert.ToDecimal(cubic));
MessageBox.Show(txtValormetrocubico.Text);
}
//this only allow numbers and "." and "," on textimbox imput
private void txtValormetrocubico_KeyPress(object sender, KeyPressEventArgs e)
{
if (!char.IsControl(e.KeyChar)
&& !char.IsDigit(e.KeyChar)
&& e.KeyChar != '.' && e.KeyChar != ',')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.'
&& (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
if (e.KeyChar == ','
&& (sender as TextBox).Text.IndexOf(',') > -1)
{
e.Handled = true;
}
}
The first time I enter a value in the text box, the value is converted to currency format perfectly, like 300
to $ 300.00
. But I edit this textbox value again and press enter, it gives an error: "Input String was not in a Correct Format" pointing to the line below:
decimal cubic = Convert.ToDecimal(txtValormetrocubico.Text);
I think the problem is caused by the fact that the value is already in decimal format. So when I click on the field and press enter again, it causes an error because the value cannot be parsed. How do I avoid this error ?
EDIT: My previous question was my first. As I am new user and don't have much knowledge in C#, I forgot to post my code. After studying some more, I made part of it work. Only this little problem remains. Please vote up, I was banned and cant make new questions because I had 7 down votes.
Thanks guys.
Upvotes: 0
Views: 9082
Reputation: 5944
The problem is that the string contains the currency symbol
private void TextBox_LeaveEvent(object sender, EventArgs e)
{
var tb = sender as TextBox;
if(tb.Text.Length>0){
decimal cubic = Convert.ToDecimal(tb.Text);
tb.Text = string.Format("{0:c}", Convert.ToDecimal(cubic));
label1.Text = tb.Text;
}
}
Above the textbox.Text is set to contain the currency information:
tb.Text = string.Format("{0:c}", Convert.ToDecimal(cubic));
Since the textbox now contains a currency symbol (like € or $) the Convert.ToDecimal
fails as soon as the TextBox_LeaveEvent fires again:
decimal cubic = Convert.ToDecimal(tb.Text);
If you bing for c# masked textbox you can find articles about masked textboxes. You cold also test if the string contains any non-number-charakters (if(tbText.IndexOf(" ") >-1){...}
)
I uploaded a very basic example to remove the currency formating to github:
string RemoveCurrencyFormating(string input)
{
if(input.IndexOf(" ") !=-1){
var money = input.Substring(0, input.IndexOf(" ")-1);
return String.Format("{0:D0}", money);
}
return ""; // Todo: add Error Handling
}
On TextBox Enter Event you can do the following:
void TextBox_EnterEvent(object sender, EventArgs e)
{
var tb = sender as TextBox;
tb.Text = RemoveCurrencyFormating(tb.Text);
}
Upvotes: 2