C# with MyEvents functionality

Fri, September 3, 2004, 05:15 AM under Whidbey | VisualStudio
If you are not aware of the MyEvents functionality that VB2005 offers, check out a screenshot and short description here.

The question is how to get the same support in C#. Well it all becomes pretty straightforward if you understand how MyEvents and the new startup model work in VB. I provided an exploration here.

So, armed with that knowledge, in a new C# Windows Application do the following:
1. Add a reference to Microsoft.VisualBasic.dll
2. Replace all the code in Program.cs with the 35 lines of code given below
3. Form1.cs. This is the main form (you could put a button that throws an exception)
4. Add a form Form2.cs. This is the splash screen, no code needed.
5. Build in release mode and run from explorer.

Here is the code:
namespace WindowsApplicationCSusingVB{

public class MyApplication :
System.Windows.Forms.WindowsFormsApplicationBase{

public MyApplication():
base(System.Windows.Forms.AuthenticationMode.Windows){

base.NetworkAvailabilityChanged += new
Microsoft.VisualBasic.MyServices.NetworkAvailableEventHandler
(this.MyApplication_NetworkAvailabilityChanged);

base.Shutdown +=
new System.Windows.Forms.ShutdownEventHandler
(this.MyApplication_Shutdown);

base.UnhandledException += new
System.Windows.Forms.UnhandledExceptionEventHandler
(this.MyApplication_UnhandledException);

base.Startup += new
System.Windows.Forms.StartupEventHandler
(this.MyApplication_Startup);

this.IsSingleInstance = false;
this.EnableVisualStyles = true;
this.ShutdownStyle =
System.Windows.Forms.ShutdownMode.AfterMainFormCloses;
}

protected override void OnCreateMainForm(){
this.MainForm = new Form1();
}

protected override void OnCreateSplashScreen(){
this.SplashScreen = new Form2();
}

private void MyApplication_NetworkAvailabilityChanged(object sender,
Microsoft.VisualBasic.MyServices.NetworkAvailableEventArgs e){

System.Windows.Forms.MessageBox.Show("network");
}

private void MyApplication_Shutdown(object sender, System.EventArgs e){
System.Windows.Forms.MessageBox.Show("shutdown");
}

private void MyApplication_Startup(object sender,
System.Windows.Forms.StartupEventArgs e){
System.Windows.Forms.MessageBox.Show("startup");
}

private void MyApplication_UnhandledException(object sender,
System.Windows.Forms.UnhandledExceptionEventArgs e){
System.Windows.Forms.MessageBox.Show(
e.Exception.StackTrace, e.Exception.Message);
e.ExitApplication = false;
}

internal static void Main(string[] Args){
(new MyApplication()).Run();
}
} //end class
} //end namespace
Comments are closed.