The syntax is quite wrong. Anyway there is an easier way to do what you want:
private void Botão_Click(object sender, EventArgs e) {
PBox.Visible = !Pbox.Visible;
}
I put in the Github for future reference.
If I were to write in the form of the question it should be so:
if (Pbox.Visible) {
Pbox.Hide();
} else {
Pbox.Show();
}
The methods Hide()
and Show()
do not return anything so cannot be used as a condition of if
that expects a boolean. To know if the control is visible use the property Visible
. It is not necessary to use a else
since this property can only return two valore, or is true, or is false, if true executes the block of the if
, otherwise it will execute the else
, independent of anything else. Even if it were the case to use a condition, it could only with a else if
.
What a mess of code.
else
has no associated condition, in conditionif
can’t have;
. Then there’s the problem of what you want to do that feels wrong. But before you see what you want to do you should learn the syntax of the language.– Jorge B.