Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# SQL-Server
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 17-Jan-13 0:41am
Edited 17-Jan-13 1:25am
v2
Comments
Anele Ngqandu at 17-Jan-13 7:40am
   
what is the format of the value you converting in textbox3?
selva_1990 at 17-Jan-13 7:41am
   
simply three empty textbox control
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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:
 
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.
  Permalink  
v2
Comments
selva_1990 at 17-Jan-13 9:51am
   
thank u very much for your help Mr.fred :) i have one more doubt in this concept
selva_1990 at 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 at 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 at 17-Jan-13 10:14am
   
yes i got it... :) :) u helped at right time
Fred Flams at 17-Jan-13 10:19am
   
you're welcome, always a pleasure to help
selva_1990 at 17-Jan-13 10:22am
   
:)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

HI,
 
Change your code to :
 
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
  Permalink  
v3
Comments
selva_1990 at 17-Jan-13 7:10am
   
your code showing lot of error
selva_1990 at 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
selva_1990 at 17-Jan-13 7:37am
   
again 6 error
selva_1990 at 17-Jan-13 7:43am
   
your updated also showing error can u correct
selva_1990 at 17-Jan-13 9:47am
   
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 28 21 addition logic
 
Error 2 Argument '2' must be passed with the 'out' keyword H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 28 49 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 33 21 addition logic
 
Error 4 Argument '2' must be passed with the 'out' keyword H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 33 49 addition logic
 
Error 5 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 48 25 addition logic
 
Error 6 Argument '2' must be passed with the 'out' keyword H:\stuDieS\karthik\selvakumar\addition logic\addition logic\Form1.cs 48 52 addition logic
selva_1990 at 17-Jan-13 10:05am
   
ya its fine thank you for kind response :)
selva_1990 at 17-Jan-13 7:34am
   
can u help me
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
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();
 	}
}
  Permalink  
Comments
selva_1990 at 17-Jan-13 7:24am
   
again error
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

hi,
 
make sure that there is some value in the textBox3 and that is numeric to avoid this error
  Permalink  
v5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
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 = a - b;
               textBox3.Text = c.ToString();
  Permalink  
v4
Comments
selva_1990 at 17-Jan-13 7:09am
   
showing error as Input string was not in a correct format in this int a = Convert.ToInt16(textBox1.Text);
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

try this code :
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();
           }
       }
  Permalink  

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



Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid