When to use .PerformClick()
-
When I try to click a button programmatically using .performClick() it only works when the button is in the same class // this works this.buttonStart.PerformClick(); But if I try to use the perform click outside the class it doesn't work // this doesn' work FormCalendarCS FCCS = new FormCalendarCS(); FCCS.buttonStart.PerformClick(); However if I use the following code I can access the button's method as follows, but doesn't click the button: // this works FCCS.buttonStart_Click(sender, e); Why is this so, and how do I click on the button in another class. I appreciate any help, thanks in advance. Mihael
-
When I try to click a button programmatically using .performClick() it only works when the button is in the same class // this works this.buttonStart.PerformClick(); But if I try to use the perform click outside the class it doesn't work // this doesn' work FormCalendarCS FCCS = new FormCalendarCS(); FCCS.buttonStart.PerformClick(); However if I use the following code I can access the button's method as follows, but doesn't click the button: // this works FCCS.buttonStart_Click(sender, e); Why is this so, and how do I click on the button in another class. I appreciate any help, thanks in advance. Mihael
It seems like you have made
buttonStart_Click
public static
in yourFormCalendarCS
class which is why you can access it (probably not a good idea!), whereasbuttonStart
is an instance created in the designer of yourFormCalendarCS
(exactly as it should be, check the contents of the designer.cs file). Creating a new instance of the form will not get you the original button, just a new one on a new form that you haven't made visible so you will never see it. First of all, changebuttonStart_Click
toprivate void buttonStart_Click(object sender, EventArgs e)
, there is no valid reason to have it any other way IMO, and if you have changed it, set the access modifier onbuttonStart
back toprivate
. InsideFormCalendarCS
create this method:public void ButtonStartPerformClick()
{
buttonStart.PerformClick();
}Inside your form where you first show your FormCalendarCS instance, add a field:
private FormCalendarCS formCalendarCS;
Before you show it, save the instance to the field.
formCalendarCS = new FormCalendarCS();
formCalendarCS.Show();Now whenever you want to perform the click, just call:
formCalendarCS.ButtonStartPerformClick();
If the relationships between your classes/forms are more complex, you will need to route the method call(s) appropriately and you may need to use events too if you need to call back up the chain. Your problem boils down to not understanding instance vs static objects and methods. These are a fundemental part of any OOP language including C# so I suggest some study on this area.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
It seems like you have made
buttonStart_Click
public static
in yourFormCalendarCS
class which is why you can access it (probably not a good idea!), whereasbuttonStart
is an instance created in the designer of yourFormCalendarCS
(exactly as it should be, check the contents of the designer.cs file). Creating a new instance of the form will not get you the original button, just a new one on a new form that you haven't made visible so you will never see it. First of all, changebuttonStart_Click
toprivate void buttonStart_Click(object sender, EventArgs e)
, there is no valid reason to have it any other way IMO, and if you have changed it, set the access modifier onbuttonStart
back toprivate
. InsideFormCalendarCS
create this method:public void ButtonStartPerformClick()
{
buttonStart.PerformClick();
}Inside your form where you first show your FormCalendarCS instance, add a field:
private FormCalendarCS formCalendarCS;
Before you show it, save the instance to the field.
formCalendarCS = new FormCalendarCS();
formCalendarCS.Show();Now whenever you want to perform the click, just call:
formCalendarCS.ButtonStartPerformClick();
If the relationships between your classes/forms are more complex, you will need to route the method call(s) appropriately and you may need to use events too if you need to call back up the chain. Your problem boils down to not understanding instance vs static objects and methods. These are a fundemental part of any OOP language including C# so I suggest some study on this area.
Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Thanks Dave, I took your advice and did as you suggested, however, the button isn't static but was changed to public. I changed it back to private and accessed the button as you said. I also understand what you mean by a new instance of that class, I seem to forget that as I have did that more than once, only to realize my mistake. Michael
-
When I try to click a button programmatically using .performClick() it only works when the button is in the same class // this works this.buttonStart.PerformClick(); But if I try to use the perform click outside the class it doesn't work // this doesn' work FormCalendarCS FCCS = new FormCalendarCS(); FCCS.buttonStart.PerformClick(); However if I use the following code I can access the button's method as follows, but doesn't click the button: // this works FCCS.buttonStart_Click(sender, e); Why is this so, and how do I click on the button in another class. I appreciate any help, thanks in advance. Mihael
I've never found a reason to use PerformClick, ever. The code you put in the event handler really needs to be moved to it's own method. Then you can call it from the button Click event handler or from any outside caller without even touching the button.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
I've never found a reason to use PerformClick, ever. The code you put in the event handler really needs to be moved to it's own method. Then you can call it from the button Click event handler or from any outside caller without even touching the button.
A guide to posting questions on CodeProject[^]
Dave KreskowiakI've never had a reason to use it either - the only advantage is it actually performs the click and so raises any other handlers attached to the
Button
'sClick
event whereas calling the handling code would not do that.Dave
Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn) -
When I try to click a button programmatically using .performClick() it only works when the button is in the same class // this works this.buttonStart.PerformClick(); But if I try to use the perform click outside the class it doesn't work // this doesn' work FormCalendarCS FCCS = new FormCalendarCS(); FCCS.buttonStart.PerformClick(); However if I use the following code I can access the button's method as follows, but doesn't click the button: // this works FCCS.buttonStart_Click(sender, e); Why is this so, and how do I click on the button in another class. I appreciate any help, thanks in advance. Mihael
MAW30 wrote:
it only works when the button is in the same class
Also,
PerformClick()
won't work if the button is disabled. /raviMy new year resolution: 2048 x 1536 Home | Articles | My .NET bits | Freeware ravib(at)ravib(dot)com