views:

79

answers:

4

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 the 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.

+6  A: 

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

digEmAll
+1 for the alternative to PerformClick. PerformClick is a hackish approach in production code. I think it should generally be limited to testing.
Greg D
My fault; I assumed it was a button, but it's actually a link label that acts like a button.Is there a similar method for those?
Raven Dreamer
@digEmAll: http://msdn.microsoft.com/en-us/library/bb337532.aspx, but as you said, a separate method is better.
Yuriy Faktorovich
@Yuriy: many thanks, I didn't know that. I've modified my post ;)
digEmAll
+2  A: 

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();

Yuriy Faktorovich
A: 

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();
}
ma7moud
A: 
private void MyMethod()
{
   // ...

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

   // ...
}

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