views:

480

answers:

4

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.

+1  A: 

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!");
}
Josh Einstein
I can not do like that in this code..
Harikrishna
Okay... Why not?
Josh Einstein
Because If I call the method directly which I am calling from the button's click() event it is not handled or not performed..
Harikrishna
A: 

Maybe I'm not understanding the question, but you can just set the button visible to false.

button1.Visible = false;
sadboy
If I make the button visible false then the button's performclick() event is not performed it is the problem.
Harikrishna
+1  A: 

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)

thedugas
A: 

There are two ways you can effectively make the Button not visible, but still be able to call its PerformClick() Event :

  1. move the Button outside the visible are of the Form or other Container object it's in, by changing its Location property.

  2. 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 ?

BillW