The way you ask it:
public class MyButton : Button {
internal void DoInvokeOnClick() {
InvokeOnClick(this, new EventArgs());
}
}
MyButton button = new MyButton();
Timer MyTimer
Timer.Tick += (sender, eventArfs) => {
button.DoInvokeOnClick();
};
That was according to your requirements, formally. But you should not place such requirements. Why creating a
Button
derived class? (This is the only way to expose
InvokeOnClick
, as it is protected.) The reasonable way is abstracting out the action you do on click. So, not following you requirement literally will be much better:
Button button = new MyButton();
Timer MyTimer
void MyClickAction() { }
button.Click += (sender, eventArfs) => { MyClickAction(); }
Timer.Tick += (sender, eventArfs) => {
MyClickAction();
};
This is actually what Albin suggested his Answer when he questioned "why raising event?" Much easier!
Now, a big warning for you. Don't use timer, especially
System.Windows.Forms.Timer
if you want to stay out of trouble. In worst case, use
System.Timers.Timer
. Still a lot of trouble. Did you ever plan for situations when a new timer tick comes when you process of the previous tick is not finished? The are sources of time troubles which are hard to detect.
Instead, use thread. To get you an idea, see my collection of past Questions on the topic and discussions:
Multple clients from same port Number[
^].
—SA