Click here to Skip to main content
15,566,394 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I am having a really big problem that is bugging me, and it seems like it only needs a simple change, but I don't know what! I am using Visual Studio 2010.

Here is my code:

C#
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if(checkBox1.Checked)
    {
        FlashingName = true;
        timer1.Enabled = true;
        timer1.Start();
    }
    else

        FlashingName = false;
        timer1.Enabled = false;
        timer1.Stop();

    }



The problem is the very last "}".
Posted
Updated 27-Jul-13 0:03am
v2
Comments
Peter Phiri 2022 3-Nov-22 7:36am    
Question 1
Write pseudocode and design also a flowchart for a program that sets a variable named username as "MyUserName" and another variable named password as 'MyPassword' then asks the user to enter the username and password. If the correct username and password are entered the program should display "Logged in" otherwise the program should display either "Incorrect Password" "Incorrect Username" as appropriate. It should also allow the user to make another attempt at entering the correct combination. The user should be allowed a maximum of THREE attempts to get the correct password

You should add a bracket { after the else keyword. You should also add another bracket at the end of your method:
C#
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if(checkBox1.Checked)
    {
        FlashingName = true;
        timer1.Enabled = true;
        timer1.Start();
    }
    else
    {   // add this bracket
        FlashingName = false;
        timer1.Enabled = false;
        timer1.Stop();
 
    }
} // also add this bracket

[EDIT]

By the way, timer1.Enabled = true; does the same as timer1.Start(); and timer1.Enabled = false; does the same as timer1.Stop();
So you can simply optimize your code by writing this:
C#
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    FlashingName = checkBox1.Checked;
    timer1.Enabled = checkBox1.Checked;
}

If checkBox1.Checked is true, then FlashingName and timer1.Enabled will be true also. Same if checkBox1.Checked is false: tiemr1.Enabled and FlashingName will be false
 
Share this answer
 
v2
Comments
ChopzModz 27-Jul-13 5:13am    
Okay, I've added what you have told me to correctly, but it still has a wiggly red line to right of the last "}" and I still have the same error.

Heres the code:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
FlashingName = true;
timer1.Enabled = true;
timer1.Start();
}
else
{
FlashingName = false;
timer1.Enabled = false;
timer1.Stop();

}
}
Thomas Daniels 27-Jul-13 5:15am    
Did you recompile the code? The code that you wrote in your comment should be OK.
But please see my edit. You can easily change your code into this:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
FlashingName = checkBox1.Checked;
timer1.Enabled = checkBox1.Checked;
}

Explanation: see my answer
Peter Phiri 2022 3-Nov-22 7:42am    
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if(checkBox1.Checked)
{
FlashingName = true;
timer1.Enabled = true;
timer1.Start();
}
else
{
FlashingName = false;
timer1.Enabled = false;
timer1.Stop();
}
}
Yes. It's missing!

Your method must start, and end with a "}", but I suspect from the indentation, that what you've actually done is forgotten the opening bracket of the else condition:
C#
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if(checkBox1.Checked)
    {
        FlashingName = true;
        timer1.Enabled = true;
        timer1.Start();
    }
    else
    {
        FlashingName = false;
        timer1.Enabled = false;
        timer1.Stop();
    }
}
 
Share this answer
 
Basically, I looked further up in my code and where there were different "}", you just have to add more at the bottom of your code, but in line with them. Hope this makes sense :)

C#
private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                FlashingName = true;
                timer1.Enabled = true;
                timer1.Start();
            }
            else
            {
                FlashingName = false;
                timer1.Enabled = false;
                timer1.Stop();

            }
        }
    }
}
 
Share this answer
 
Comments
Richard MacCutchan 27-Jul-13 11:25am    
Not really. It is not just a matter of adding braces until your code compiles. You need to understand what they are there for and when and where a block starts and ends.
CHill60 27-Jul-13 15:37pm    
I agree with Richard MacCutchan - you need to understand the concept of a block. Your solution as it stands won't compile so if yours does compile then you are going to get some unexpected results. As for your comment "in line with them" ... indentation is irrelevant - it's the fact that they match that is important

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