Search code examples
c#.netvb.netwinformsapplication-framework

Where is the Winforms "application framework" in C# that exists in VB.NET?


I've developed VB.NET Winforms projects in the past that use the "Windows Application Framework" settings, which are found on the "Application" tab in the project properties:

enter image description here

One particularly useful feature of this is the ability to make a single-instance application with one click of a checkbox. This works very well. Here is the C# project properties for a Winforms project:

enter image description here

There's nothing about the application framework, neither on the "Application" tab nor elsewhere. All the references I've found to "single instance" applications talk about using a custom solution with a mutex. I can't find any information about why there is no equivalent application framework in C# projects. Can anyone shed some light on this?


Solution

  • These properties are exclusively available in VB.Net project types. If you want to utilize these VB.Net properties in C# project then you need to add reference to Microsoft.VisualBasic assembly and create your custom App class inherited from Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase to access the protected members that appears in the project properties and run your windows form application via your custom app class instead of C# Application class methods.

    static class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            var app = new MyApp(new Form1());
            app.Run(args);
        }
    }
    
    internal class MyApp : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        public MyApp(Form mainForm)
        {
            this.EnableVisualStyles = true;
            this.SaveMySettingsOnExit = true;
            this.IsSingleInstance = true;
            this.MainForm = mainForm;
        }
    }
    

    Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase provides predefined features to an winform application that are only available via VB.Net. There many predefined class libraries in Microsoft.VisualBasic assembly that are not available in C#.