0

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

1
  • On which line you get this error exactly? And what are the values of txtQuantity.Text and txtRate.Text ? Commented Dec 25, 2013 at 7:38

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

just returning from event handler if string is empty , very clear :)
:-) @SudhakarTillapudi :-)
3

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;

8 Comments

attaching and detaching events is expensive. any other elegant solution you have?
@Aniket: i agree with you, let me edit my answer with solution2.
@Aniket: edited my answer with Solution2 , i think this should help the OP.
You are adding an extra state, that's usually a bad thing to do..keep the programs state-free and you will definitely produce some good, failproof code.
@Aniket: sorry i didnt get it adding an extra state, is it in 2nd solution?
|
1

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);   
  }   
}

Comments

1

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.

Comments

0

You are not using the right control in your application. Please use NumericUpDown control instead.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.