Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
C#
private void textBox3_MouseClick(object sender, MouseEventArgs e)
        {
            int a = Convert.ToInt16(textBox1.Text);
            int b = Convert.ToInt16(textBox2.Text);
            if (textBox1.Text==""||a==0)
            {
                MessageBox.Show("enter value");
            }
            else if (textBox2.Text == "" || b == 0)
            {
                MessageBox.Show("enter value to sub");
            }
            else
            {
                int c = Convert.ToInt16(textBox3.Text);//getting error here i/p string not in correct format
                c = a - b;
                textBox3.Text = c.ToString(); 
            }
        }


i am having 3 textbox a,b,c and need to perform simple subtraction with valitation like if a and b has "0" or "null" value it need to show messagebox as enter value... and if all ok subtraction need to perform...
but my code showing error pplz help me
Posted
Updated 17-Jan-13 1:25am
v2
Comments
Anele Ngqandu 17-Jan-13 7:40am    
what is the format of the value you converting in textbox3?
selva_1990 17-Jan-13 7:41am    
simply three empty textbox control

Hi,

If I have correctly understood your problem, you are trying to populate a textbox (TB3) using the result of the substraction between two integers input (using TB1 and TB2), then you should try the following code snippet:

C#
private void textBox3_MouseClick(object sender, MouseEventArgs e)
{
    int a = 0, b = 0, c = 0;

    if (!int.TryParse(textBox1.Text,
                        System.Globalization.NumberStyles.Integer,
                        System.Globalization.CultureInfo.CurrentUICulture,
                        out a))
    {
        MessageBox.Show("enter value");
    }
    else if (!int.TryParse(textBox2.Text,
                            System.Globalization.NumberStyles.Integer,
                            System.Globalization.CultureInfo.CurrentUICulture,
                            out b))
    {
        MessageBox.Show("enter value to sub");
    }
    else
    {
        // If I have correctly understood what you are trying to achieve
        // There is no need to parse the current value of TB3, since it will always
        // be overriden with the result of your operation
        c = a - b;
        textBox3.Text = c.ToString(
            System.Globalization.CultureInfo.CurrentUICulture);
    }
}


The TryParse function returns a boolean indicating whether the input string has been parsed into a valid integer. If it returns false, then the out parameter is unchanged from its initial value.

I have used the overload of TryParse that takes in the number style and format provider so that it works on any desktop computer indifferently of its localization (US, European, Russian, Asian, and so on...)

Hope this helps.
 
Share this answer
 
v2
Comments
selva_1990 17-Jan-13 9:51am    
thank u very much for your help Mr.fred :) i have one more doubt in this concept
selva_1990 17-Jan-13 9:56am    
if i give number greater then first textbox its showing minus value(negative value) instead of that,i need second textbox should not accept "value" greater then first textbox
Fred Flams 17-Jan-13 9:59am    
then in the second "if" you should add the following condition "|| b > a" and change the text of the message box to read something like "enter value to sub lower or equal to first value" and that should do the trick
selva_1990 17-Jan-13 10:14am    
yes i got it... :) :) u helped at right time
Fred Flams 17-Jan-13 10:19am    
you're welcome, always a pleasure to help
At the line where you are getting error, textbox3 text is not yet set. You can remove that line of code.

There are still few more issues with the code but I will leave that to you to figure out.
 
Share this answer
 
HI,

Change your code to :

C#
private void textBox3_MouseClick(object sender, MouseEventArgs e)
        {
int b = 0;
int a = 0;
int c = 0;
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                
                int.TryParse(textBox1.Text,a);
            }
            if (string.IsNullOrEmpty(textBox1.Text))
            {
                
                int.TryParse(textBox2.Text,b);
            }
            if (a == 0)
            {
                MessageBox.Show("enter value");
            }
            else if (b == 0)
            {
                MessageBox.Show("enter value to sub");
            }
            else
            {
                if (string.IsNullOrEmpty(textBox1.Text))
                {
                    
                    int.TryParse(textBox3.Text, c); //getting error here i/p string not in correct format
                }
                else
                {
                     c = 0;
                }
                c = a - b;
                textBox3.Text = c.ToString();
            }
        }


Thanks
 
Share this answer
 
v3
Comments
selva_1990 17-Jan-13 7:10am    
your code showing lot of error
[no name] 17-Jan-13 7:13am    
which line you are getting error and what are the errors ?
selva_1990 17-Jan-13 7:21am    
Error 1 The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 23 25 addition logic
Error 2 Argument '2' must be passed with the 'out' keyword H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 23 53 addition logic
Error 3 The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 27 25 addition logic
Error 4 Argument '2' must be passed with the 'out' keyword H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 27 53 addition logic
Error 5 The name 'a' does not exist in the current context H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 29 17 addition logic
Error 6 The name 'b' does not exist in the current context H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 33 22 addition logic
Error 7 The best overloaded method match for 'int.TryParse(string, out int)' has some invalid arguments H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 41 29 addition logic
Error 8 Argument '2' must be passed with the 'out' keyword H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 41 57 addition logic
Error 9 The name 'c' does not exist in the current context H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 47 17 addition logic
Error 10 The name 'a' does not exist in the current context H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 47 21 addition logic
Error 11 The name 'b' does not exist in the current context H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 47 25 addition logic
Error 12 The name 'c' does not exist in the current context H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 48 33 addition logic
[no name] 17-Jan-13 7:34am    
Check my Updated solution.
selva_1990 17-Jan-13 7:37am    
again 6 error
Replace with the following code
Before converting into the integer, please make sure the textbox3 is not empty. if it is empty u can not convert empty ("") value into integer.
C#
else
{
	if(textBox3.Text!="")
	{
                int c = Convert.ToInt16(textBox3.Text);//getting error here i/p string not in correct format
                c = a - b;
                textBox3.Text = c.ToString();
 	}
}
 
Share this answer
 
Comments
selva_1990 17-Jan-13 7:24am    
again error
hi,

make sure that there is some value in the textBox3 and that is numeric to avoid this error
 
Share this answer
 
v5
why you are assigning empty value to 'c' you just have to display the resultant value of subtraction i.e, 'C' value in textbox right!. Then do the subtraction then display the c value to the text box.
C#
int c = Convert.ToInt16(textBox3.Text);//getting error here i/p string not in correct format
                c = a - b;
                textBox3.Text = c.ToString();



the above code is wrong. Try this below code,
C#
c = a - b;
               textBox3.Text = c.ToString();
 
Share this answer
 
v4
Comments
selva_1990 17-Jan-13 7:09am    
showing error as Input string was not in a correct format in this int a = Convert.ToInt16(textBox1.Text);
try this code :
C#
private void textBox3_MouseClick(object sender, MouseEventArgs e)
       {
           string a = textBox1.Text;
           string b = textBox2.Text;
           int t=0;
           int z = 0;
           if (a != "" && b != "")
           {
               t = Convert.ToInt16(textBox1.Text);
               z = Convert.ToInt16(textBox2.Text);
           }
           else { MessageBox.Show("enter value"); }
           if (t == 0)
           {
               MessageBox.Show("enter value");
           }
           else if (z == 0)
           {
               MessageBox.Show("enter value to sub");
           }
           else
           {
               int c = t - z;
                textBox3.Text = c.ToString();

               textBox3.Text = c.ToString();
           }
       }
 
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