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 :

public MyApplication():

base.NetworkAvailabilityChanged += new

base.Shutdown +=
new System.Windows.Forms.ShutdownEventHandler

base.UnhandledException += new

base.Startup += new

this.IsSingleInstance = false;
this.EnableVisualStyles = true;
this.ShutdownStyle =

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){


private void MyApplication_Shutdown(object sender, System.EventArgs e){

private void MyApplication_Startup(object sender,
System.Windows.Forms.StartupEventArgs e){

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

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