My "crystal ball" is cloudy, but ... I'm going to guess that the "static method" needs a reference to the instances of the custom controls (going to assume they are User Controls) which it doesn't have now ... since a Control, by definition, cannot be 'static.'
Another guess might be the static method is in a static class that, perhaps, is a 'factory' that returns instances of your custom control on-demand that are then "sited" in some run-time container.
If you are speaking about adding instances of your custom control at run-time:
1. define a static collection of type 'YourCustomControl,' and then, at the place in the code where your custom control is created, add it to that collection. Then, of course, you make that collection accessible to the static method that can enumerate it, and invoke the Refresh() method on each custom control.
Something to think about: if the static method is not "defined in" the object where the custom controls are created: how do you make it accessible to the instance ... for example, a Form ... where the new custom controls are created ?
If you are speaking about adding instances of your custom control at design-time:
Assuming you are drag-dropping your custom control into some container object (Form, Panel, etc.), then you are going to need to do something in the custom control code that does the right thing to add the new instance to the static list of instances.
Is there a 'happy solution' which will work for both design-time and run-time ? Well, I think so :)
1. Because you may have instances of your custom controls on more than one Form, or, in one Form or other container inside another (nested), my choice would be to make a static class that encapsulates what you need to do to all instances of the custom controls everywhere.
public static class TheStaticClass
{
public static List<YourCustomControl> YCCList = new List<YourCustomControl>();
public static void RefreshYCCs()
{
foreach (YourCustomControl ycc in YCCList)
{
ycc.Refresh();
}
}
}
2. Now you can take advantage of the Load event of your custom control to update the List which will work at design-time, or when you create instances at run-time:
private void YourCustomControl_Load(object sender, EventArgs e)
{
TheStaticClass.YCCList.Add(this);
}
3. And now from somewhere else (inside some other Form, or whatever) in your code you can invoke the refresh of all instances of your custom control:
private void button1_Click(object sender, EventArgs e)
{
TheStaticClass.RefreshYCCs();
}
4. In this scenario my personal preference would be to move the code (if any) that creates a new instance of the custom control
at run-time into the static class (i.e., make it a 'factory' class), and when I called the method in the static class, I'd pass it the container the new instance is to be inserted into ... but that's a matter of taste, or style.
public static void AddNewCustomControl(Control targetControl)
{
targetControl.Controls.Add(new YourCustomControl ());
}
Note that in outlining this little snippet I did not consider the possibility there might be some type of container object into which you might wish to insert an instance of your custom control that just might not have its internal ControlsCollection accessible in the 'usual' way. Paranoia is the friend of pest-control ?
~
There are other strategies implied here, and I'm sure you'll hear some 'other visions' of those strategies, and others that I never dreamed of, on this thread :)