Hello,
I was given the assignment to create a C# program in visual studio.net that has a form with a button on it, and when the button is clicked, the form will double in size. Also, when the button is clicked again, the form will go back to its normal size. I tried to do an if/else statement but that did not seem to work. Here is what I have so far, any help would be great:
private
void button2_Click(object sender, EventArgs e){
if (this.Width == 300) this.Width +=300; this.Height +=200; else this.Width -=300; this.Height -=200;}
I'm getting the error: invalid expression term 'else' which I cannot determine why.
Jan MontanoPosted Aug 29, 2007, 9:48 PM
It should be...
private void button2_Click(object sender, EventArgs e)
{
}
Having no braces after your if condition means that you will be executing only 1 line of code after the if condition
if (this.Width == 300)
this.Height +=200;
your code is read like that so when .net encounters the else statement afterwards, it returns an error because it was not expecting it. this.Height +=200; will execute regardless of the if condition because it's not inside the if statement. You should put them inside the curly braces instead.