PerformClick() Event Of The Button
I have a button on the window form and and one method I am calling button's PerformClick() event.
Now if I make this button visible false.Then that PerformClick() event is not performed.
Is it ok? And if yes then I want to make the button invisible on the form what should I do ? I already make the background color of the button same as 开发者_运维知识库the form backcolor but it remains visible.
If I use the label then it is possible to make it invisible other than changing its visible property by just make its backcolor same as the form's backcolor and keep the text null.And there is also the click() event of the label but I don't find the PerformClick() event of the label.
PerformClick specifically checks to see if the button is "available" before raising any events. This is so that you don't have to put extra guard code for when the button is disabled, hidden, etc.
Instead of calling the PerformClick method on the button, why not just take the code in the button's Click event handler and put it into a method that you then call from both places.
private void button1_Click(object sender, EventArgs e) {
DoSomething();
}
private void menu_Click(object sender, EventArgs e) {
DoSomething();
}
private void DoSomething() {
MessageBox.Show("Something done!");
}
I am not sure if I understand your problem, but below are some posts that might(?) give you an answer:
See this post (similar question) and this post (transparent button)
There are two ways you can effectively make the Button not visible, but still be able to call its PerformClick() Event :
move the Button outside the visible are of the Form or other Container object it's in, by changing its Location property.
remove the Button from the Controls collection of the Form or other Container object it's in.
And yes, with both of those techniques you can "bring back" the Button : in the first case by re-setting the Location property; in the second case by adding the Button back to the Form or other Container object that it was removed from (which will restore the Location).
Both of those techniques I believe are "terrible" : they are hacks : Josh Einstein's answer above, I believe is putting you on the right track.
imho you need to explain why you said, in response to Josh's suggestion you separate out the code into a "shared method" : "Because If I call the method directly which I am calling from the button's click() event it is not handled or not performed"
Why isn't the method "handled" or "performed" when you call it directly ?
I had a similar problem and i found a different way to solve. To help the other guys here:
You can use the Control (form) Method InvokeOnClick to ForceClick an Visible=False button
this.InvokeOnClick(button1, EventArgs.Empty);
If you don't have access to Form, you can try button1.Parent.InvokeOnClick...
Thanks (Posted in other forum by Stoitcho Goutsev)
Raffaeu reminded me that "control.InvokeOnClick is a protected method"
Maybe I'm not understanding the question, but you can just set the button visible to false.
button1.Visible = false;
精彩评论