Post: c# Doing Something With Characters In Text Box
10-02-2015, 02:51 AM #1
Trefad
I defeated!
(adsbygoogle = window.adsbygoogle || []).push({}); How would I go about making an if statement with a number. Example... if(textBox1 = 1)... Only that part. I always get errors when I try. It says that it cannot convert int to a windows form. Hope some one knows Smile
(adsbygoogle = window.adsbygoogle || []).push({});
10-02-2015, 02:59 AM #2
Specter
Pro Memer
Originally posted by Trefad View Post
How would I go about making an if statement with a number. Example... if(textBox1 = 1)... Only that part. I always get errors when I try. It says that it cannot convert int to a windows form. Hope some one knows Smile


You have multiple problems in that code, one is textBox1 itself IS a windows form, what you want is the .text property. Another is you need a double equals (==), not a single equals (=). One equal sign signifies assignment, for a comparison you want to use a double. Now since you want to compare directly with an integer you'd want to cast textBox1's text as an integer as well, to do this you would use the following code:

    int myValue = int.Parse(textBox1.Text); /* rename myValue to a suitable variable name */

if(myValue == 1)
{
/* code here */
}


Edit: If you wanted to get secure with it to prevent unpredictable / desirable results then you can write up a little check function to ensure textBox1 only contains an integer value, as if a string is passed in and your program tries to convert it, as I said you might get unpredictable results.

The following user thanked Specter for this useful post:

Trefad
10-02-2015, 03:39 AM #3
Trefad
I defeated!
Thank you so much! Smile
10-03-2015, 08:42 AM #4
Mango_Knife
In my man cave
Originally posted by Trefad View Post
How would I go about making an if statement with a number. Example... if(textBox1 = 1)... Only that part. I always get errors when I try. It says that it cannot convert int to a windows form. Hope some one knows Smile


== For comparasion You must login or register to view this content.
= To say equal to something You must login or register to view this content.

And you can use int.Parse You must login or register to view this content.
    
int valueOfTextBox = int.Parse(textBox1.Text);
if (valueOfTextBox == 1)
{
}

This is the same i've just didnt defined the integer
                if (int.Parse(textBox1.Text) == 1)
{
}
10-03-2015, 04:05 PM #5
Trefad
I defeated!
Thanks Smile
10-23-2015, 04:02 AM #6
Question certainly answered.

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo