Jump to content

Hey some simple C# code help!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
11 replies to this topic

#1
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
MessageBox.Show("Hello World");

            

            i++;

            if (i > 1)

           label1.Text = "You clicked me " + i++ + " times!";


            else

            label1.Text = "You clicked me 1 time!";


            if (checkBox1 == checked);

            label1.Text = "You clicked me " + i + " times";

Im just messing around with code to see what I can try out, but I am trying to make it so you click a button, and a box pops up that says hello world. And it tells you how many times you clicked the button. But now Im trying to make it so you can have a box checked and it stops recording how many times you clicked. Thanks!

#2
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
if (checkBox1.Checked) label1.Text = "You clicked me " + i.ToString() + " times";

Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#3
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts

stopped = false;

if(checkBox1 == checked)
{
   label1.Text = "You clicked me " + i.toString() + " times!  - Updating has stopped";
   stopped = true;
}
if(!stopped && i > 1)
{ 
     i++;
     label1.Text = "You clicked me " + i.toString() + "times!";
}
else
     label1.Text = "You clecked me 1 time!";

Davide is definitely right you need the ToString() method to display an integer value as a String. (assuming that was an integer)

#4
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thanks guys! Sorry I'm just starting out, I appreciate your help

#5
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

mbridges said:

Thanks guys! Sorry I'm just starting out, I appreciate your help
Don't be sorry, this is what forums are for, to get help and learn. You could check out my tutorial (in the signature) if you want to learn the basics of C#.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#6
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Thanks for that tutorial, I will be sure to check it out!
I have another problem though.

        public void button1_Click(object sender, EventArgs e)
        {
            i++;
            if (i > 4)
                MessageBox.Show("You Win!");
            else label1.Text = "Your score is " + i;
            
            if (checkBox1 = checked);
                label1.Text = "Your score is " + i--;
            else label1.Text = "Your score is " + i;



        }

Right after the line if (checkBox1 = checked); it says in the error box "Syntax error, '(' expected. Any help guys?

#7
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts

mbridges said:

Thanks for that tutorial, I will be sure to check it out!
I have another problem though.

        public void button1_Click(object sender, EventArgs e)
        {
            i++;
            if (i > 4)
                MessageBox.Show("You Win!");
            else label1.Text = "Your score is " + i;
            
            if (checkBox1 = checked);
                label1.Text = "Your score is " + i--;
            else label1.Text = "Your score is " + i;



        }

Right after the line if (checkBox1 = checked); it says in the error box "Syntax error, '(' expected. Any help guys?

Assuming that i is an integer, you MUST convert it to a string using .ToString(), also, delete that semicolon after the if statement:
[COLOR=#000000][COLOR=#007700]            if ([/COLOR][COLOR=#0000BB]checkBox1 [/COLOR][COLOR=#007700]= [/COLOR][COLOR=#0000BB]checked[/COLOR][COLOR=#007700]);
[/COLOR][/COLOR]
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#8
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
So after the if statement it would look like

if (checkBox1 = checked).ToString()

Correct?

#9
Davide

Davide

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 506 posts
No, "checked" is a property of the text box. It is a boolean variable.
if (checkBox1.Checked == true) /* is the same thing with */ if (checkBox1.Checked)
THe if statement just checks if the relation is true.
Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

#10
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Okay thanks! I'm just trying out different things with what I know so far to get used to this stuff.

#11
While(!EOF)

While(!EOF)

    Newbie

  • Members
  • PipPip
  • 21 posts
Another thing to mention:

if(checkbox == checked) 

and

if(checkbox = checked)

are 2 different expressions! Using "==" is like saying "is the value equal to" and the single "=" is an assignment. When you are checking a value against another use the "==" and when you want to assign something a value then use the single "=".
So essentially by using

if(checkbox = checked)

you are actually saying set checkbox equal to checked and then see if checkbox is a true value. This is different from saying "is checkbox equal to the same value as checked."

Hope that helps.

Edited by While(!EOF), 15 June 2010 - 06:15 AM.
grammar - im a programmer dude give me a break


#12
mbridges

mbridges

    Learning Programmer

  • Members
  • PipPipPip
  • 33 posts
Okay thanks! Nice to know.