Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

C#
private void textBox2_TextChanged(object sender, EventArgs e)
        {
            float t;
            float g;

            t = float.Parse(textBox3.Text);
            g = float.Parse(textBox10.Text);
            float j;

            j = g * (t / 100) + g;


            Convert.ToDecimal(textBox2.Text = j.ToString());
        }

Here input string is not correct format error in textbox3.text how to resolve Help me......
Posted
Updated 22-Dec-13 0:26am
v2

Get your user to type correct values?
Or use the right textbox?
You are handling the textBox2 event, but you don;t reference textBox2 at all - I suspect you meant 2 rather than 3.

But even then, that is a poor way to treat users.
Before you use a value, check it, and report problems to your user: at the moment, if he leaves a field blank your program crashes. If he miskeys and gets a comma instead of a dot, it crashes. If he types his name, it crashes. That really is poor - it would annoy the heck out of you, wouldn't it?

Try this:
C#
private void textBox2_TextChanged(object sender, EventArgs e)
    {
    float t;
    float g;
    if (!float.TryParse(textBox3.Text, out t))
        {
        MessageBox.Show("Please type a number in textbox 3!");
        return;
        }
    if (!float.TryParse(textBox10.Text, out g))
        {
        MessageBox.Show("Please type a number in textbox 10!");
        return;
        }

    float j = g * (t / 100) + g;
    string result = j.ToString();
    if (textBox2.Text != result)
        {
        textBox2.Text = result;
        }
    }

Notice the bit at the bottom: if you just set the value of TextBox2.Text each time, it will immediately cause a new event to occur!

And do yourself a favour: stop using VS default names for controls! Calling your controls a name that describes what the user uses them for makes your code much more reliable and readable!

textBox2  => tbTaxDue
textBox10 => tbTaxRate

and so forth.
 
Share this answer
 
Comments
Richard MacCutchan 22-Dec-13 7:10am    
And, of course, don't use floats for financial calculations. ;)
OriginalGriff 22-Dec-13 7:14am    
There is that...:laugh:
Integers are so much more precise!
for financial calculation us double datataype and to resolve the problem restict them from typing alphabets and other special characters.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900