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";开发者_如何学JAVA
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;
}
}
You forgot an "else". This should work :
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);
}
else if (button3.Text == "Hide") //this is where you should put an else
{
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 Text
that is neither Help
nor Hide
. Therefore, neither if
statement 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.Text
and 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 an else
clause.
Therefore, your code will see that the button's text is Help
, and change it to Hide
.
However, the next if
statement will see that the button's text is now Hide
, and will change it back to Help
.
You have two ifs in parallel. The first block will execute, changing the button text to "Hide". Then, the second will also execute, changing the text back to "Help".
Use an else block instead.
Why don't you set a breakpoint at the start of the method and see what the value is for button3.Text?
My guess is that your event is not wired up properly and the code is not even being exectued.
Do you have an ampersand in the text of the button (e.g. &Help or &Hide) so that users can use the keyboard shortcut? If so, the text property will not be equal.
There are a couple problems with this code.
First things first though, make sure that your button's click event is properly hooked to the button2_Click
handler, as there seems to be some disparity between the name of the method, and the objects referenced within the method. The naming on these should be fixed either way for readability's sake.
Second, your code uses two if
statements, rather than an if...else
statement. If the first condition in your code is true, then the code inside the first if
will cause the condition in the second if
to be true, thus reverting everything to it's original condition.
Your code SHOULD read (including renaming your method):
private void button3_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);
}
else
{
Size = new System.Drawing.Size(586, 563);
button3.Text = helpstring;
}
}
NOTE: There is no need for a second if
if these are the only two possible conditions, a simple else
will improve performance and achieve the desired effect.
精彩评论