Reputation: 97
I've written this code
private void txtRate_TextChanged(object sender, EventArgs e)
{
try
{
decimal qty;
decimal price;
decimal amt = 0;
qty = Convert.ToDecimal(txtQuantity.Text);
price = Convert.ToDecimal(txtRate.Text);
amt = qty * price;
txtAmount.Text = amt.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
For getting an value in txtAmount
textbox automatically and I can get it correctly.
After that I want to clear all textbox, so I wrote this
txtProdSerName.Text = "";
txtQuantity.Text = "";
txtRate.Text = "";
txtAmount.Text = "";
but I am getting error
input string was not in a correct format
because textchange
event occurs again here.
PLEASE GIVE ME SOME SUGGESTION OR SOLUTION ABOUT THIS
Upvotes: 0
Views: 1592
Reputation: 3781
Ensure that your txtQuantity.Text
and txtRate.Text
is not empty because TextChangeEvent fire when you empty your textbox values.
private void txtRate_TextChanged(object sender, EventArgs e)
{
try
{
decimal qty;
decimal price;
decimal amt = 0;
if (!String.IsNullOrEmpty(txtQuantity.Text) && !String.IsNullOrEmpty(txtRate.Text))
{
qty = Convert.ToDecimal(txtQuantity.Text);
price = Convert.ToDecimal(txtRate.Text);
amt = qty * price;
txtAmount.Text = amt.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Upvotes: 1
Reputation: 25695
Change the code a little to look like this:
private void txtRate_TextChanged()
{
try
{
if(txtRate.Text.Trim() == string.Empty) return;
decimal qty;
decimal price;
decimal amt = 0;
qty = Convert.ToDecimal(txtQuantity.Text);
price = Convert.ToDecimal(txtRate.Text);
amt = qty * price;
txtAmount.Text = amt.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This way, you return out of txtRate_TextChanged()
event each time txtRate is empty.
Upvotes: 4
Reputation: 26199
Problem : TextChanged
Event will be fired everytime the Textbox
value changes. it will be fired even if you clear the TextBox
value and leads to the exception.
Solution 1: you can unsubscribe
from the TextChanged
Event before clearing the Text Feilds and subscribe
to it again after clearing the TextFeilds.so that while clearing the TextBox values it can not call the TextChanged
Event as there is no subscribed
event handler for TextChanged
.
Try This:
txtRate.TextChanged -= new System.EventHandler(txtRate_TextChanged);
txtProdSerName.Text = "";
txtQuantity.Text = "";
txtRate.Text = "";
txtAmount.Text = "";
txtRate.TextChanged += new System.EventHandler(txtRate_TextChanged);
Solution 2: you can create a boolean
variable and check if it is really a TextChanged
Event.
Step 1 : Create Boolean
variable and initialise it to true
.
Step 2 : change the Boolean
variable value to false
before clearing the Textbox
values.
Step 3 : change the Boolean
variable back to true
after clearing the Textbox
values.
Step 4 : execute your TextChanged
event handler code only if the Boolean
variable is true
.
Try This:
private bool isTextChangedEvent = true;
private void txtRate_TextChanged(object sender, EventArgs e)
{
if(isTextChangedEvent)
{
try
{
decimal qty;
decimal price;
decimal amt = 0;
qty = Convert.ToDecimal(txtQuantity.Text);
price = Convert.ToDecimal(txtRate.Text);
amt = qty * price;
txtAmount.Text = amt.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
while clearing TextBox
values:
isTextChangedEvent = false;
txtProdSerName.Text = "";
txtQuantity.Text = "";
txtRate.Text = "";
txtAmount.Text = "";
isTextChangedEvent = true;
Upvotes: 3
Reputation: 12439
Problem:
When you change the text it fires the event, and before that you are setting
txtQuantity
txtrate
to ""
, and in that event you are using txtQuantity
txtrate
values(which are actually Empty
now, so they can not be parsed to decimal
) that's why when it tries to change the text of txtRate
it finds txtQuantity
value is ""
(Empty) and throws the exception.
Solution:
Put your this code in a separate method:
private void changeTxtRateValue()
{
try
{
decimal qty;
decimal price;
decimal amt = 0;
qty = Convert.ToDecimal(txtQuantity.Text);
price = Convert.ToDecimal(txtRate.Text);
amt = qty * price;
txtAmount.Text = amt.ToString();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
and call the method only when txtQuantity
is not Empty
:
private void txtRate_TextChanged(object sender, EventArgs e)
{
if(txtQuantity.Text != String.Empty)
changeTxtRateValue();
}
Best Practise Advice:
Don't use ""
to set an string empty
, because it is not empty
.
Always use String.Empty
.
Use this code:
txtProdSerName.Text = String.Empty;
txtQuantity.Text = String.Empty;
txtRate.Text = String.Empty;
txtAmount.Text = String.Empty;
It'll solve your problem.
Upvotes: 1
Reputation: 5600
You are not using the right control in your application. Please use NumericUpDown control instead.
Upvotes: 0