A quick and dirty way is to create a MainForm link in the Program.cs file as described above.
Alternatively, you can create a static class to handle calls to the main form:
public delegate void AddStatusMessageDelegate (string strMessage); public static class UpdateStatusBarMessage { public static Form mainwin; public static event AddStatusMessageDelegate OnNewStatusMessage; public static void ShowStatusMessage (string strMessage) { ThreadSafeStatusMessage (strMessage); } private static void ThreadSafeStatusMessage (string strMessage) { if (mainwin != null && mainwin.InvokeRequired)
Put the above into the MainForm.cs file inside the namespace, but separate from your MainForm class.
Then put this call into the main class MainForm.cs.
void UpdateStatusBarMessage_OnNewStatusMessage (string strMessage) { m_txtMessage.Caption = strMessage; }
Then, when you initialize MainForm.cs, add this event descriptor to your form.
UpdateStatusBarMessage.OnNewStatusMessage += UpdateStatusBarMessage_OnNewStatusMessage;
In any UserControl or form-related form (MDI) you want to invoke, we just follow ...
UpdateStatusBarMessage.ShowStatusMessage ("Hello World!");
Since it is static, it can be called from anywhere in your program.
Paul talbot
source share