I have a simple button that when clicked should change the text from Help to Hide and the windows form size. The problem i’m having is it cant find the if statement. It cant see the button3.text Help or hide. Any tips or suggestions?
private void button2_Click(object sender, EventArgs e)
{
string helpstring = "Help";
string hidestring = "Hide";
if (button3.Text == helpstring)
{
button3.Text = hidestring;
Size = new System.Drawing.Size(1106, 563);
}
if (button3.Text == "Hide")
{
Size = new System.Drawing.Size(586, 563);
button3.Text = helpstring;
}
}
It looks like you have your buttons wrong.
The handler is named button2_Click, but the code accesses button3.
This is why you should always name your controls.
Your problem might be that the button3 is starting out with a
Textthat is neitherHelpnorHide. Therefore, neitherifstatement will do anything.You should set a breakpoint in the function (click the bar on the left next to one of your lines of code), then move the mouse over
button3.Textand see what it’s actually equal to.However, if the button’s text is
Help, nothing will happen. As manitra pointed out, you don’t have anelseclause.Therefore, your code will see that the button’s text is
Help, and change it toHide.However, the next
ifstatement will see that the button’s text is nowHide, and will change it back toHelp.