开发者

Triggering a "Click" event without user input

开发者 https://www.devze.com 2023-01-06 09:13 出处:网络
I have a Windows Forms Link Label, \"Refresh\", that refreshes the display. In another part of my code, part of a separate windows form, I have a dialog that changes the data loaded into the display

I have a Windows Forms Link Label, "Refresh", that refreshes the display.

In another part of my code, part of a separate windows form, I have a dialog that changes the data loaded into the display in t开发者_JS百科he first place. After executing this other code, pressing "Refresh" updates the data correctly.

Is there a simple way for the dialog menu to "click" the "refresh" Link Label after it has finished altering the data?

Using Visual Studio 2008.


For button is really simple, just use:

button.PerformClick()

Anyway, I'd prefer to do something like:

private void button_Click(object sender, EventArgs e)
{
   DoRefresh();
}

public void DoRefresh()
{
   // refreshing code
}

and call DoRefresh() instead of PerformClick()


EDIT (according to OP changes):

You can still use my second solution, that is far preferable:

private void linkLabel_Click(object sender, EventArgs e)
{
   DoRefresh();
}

public void DoRefresh()
{
   // refreshing code
}

And from outside the form, you can call DoRefresh() as it is marked public.

But, if you really need to programmatically generate a click, just look at Yuriy-Faktorovich's Answer


You could call the PerformClick method. But Generally it is better to have the Click event of the button call a Refresh method you write. And the menu call that method as well. Otherwise your menu depends on the button being there.

Edit:

A LinkLabel implements the IButtonControl explicitly. So you could use:

((IButtonControl)button).PerformClick();


you can use a method to refrech display, the bouton_click and the dialogBox call this method

public void refrechDate()
{
}


private void button_click(...)
{
   refrechData();
}


private void MyMethod()
{
   // ...

   // calling refresh
   this.button1_Click(this.button1, EventArgs.Empty);

   // ...
}

private void button1_Click(object sender, EventArgs e)
{
   // refresh code
}
0

精彩评论

暂无评论...
验证码 换一张
取 消