{"id":44695,"date":"2023-03-09T11:15:00","date_gmt":"2023-03-09T19:15:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=44695"},"modified":"2023-03-09T12:42:03","modified_gmt":"2023-03-09T20:42:03","slug":"update-to-winforms-vb-appframework","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/update-to-winforms-vb-appframework\/","title":{"rendered":"What&#8217;s new for the WinForms Visual Basic Application Framework"},"content":{"rendered":"<p>.NET from version .NET Core 3.1 up to .NET 7 has plenty of advantages over .NET\nFramework: it provides performance improvements in almost every area, and those\nimprovements were an ongoing effort over each .NET version. The\nlatest improvements in <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/performance-improvements-in-net-6\/\">.NET\n6<\/a> and\n<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/performance_improvements_in_net_7\/\">.NET\n7<\/a> are\nreally worth checking out.<\/p>\n<p>Migrating your Windows Forms (WinForms) Visual Basic Apps to .NET 6\/7+ also\nallows to adopt modern technologies which are not (or are no longer) supported in .NET\nFramework. <a href=\"https:\/\/learn.microsoft.com\/ef\/core\/\">EFCore is one example<\/a>: it is\na modern Entity Framework data access technology that enables .NET developers to\nwork with database backends using .NET objects. Although it is not natively\nsupported for VB by Microsoft, it is designed in a way that it is easy for the\ncommunity to build up on it and provide <a href=\"https:\/\/github.com\/efcore\/EFCore.VisualBasic\">code generation support for additional\nlanguages like Visual Basic<\/a>. In\nthat context there are also changes and improvements in the new WinForms\nOut-of-Process Designer for .NET, <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/databinding-with-the-oop-windows-forms-designer\/\">especially around Object Data\nSources<\/a>.\nFor the WinForms .NET runtime, there are a series of improvements in different\nareas which have been introduced with the latest releases of .NET:<\/p>\n<ul>\n<li>\n<p>.NET 5 <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-in-windows-forms-runtime-in-net-5-0\/#new-taskdialog-control\">introduced the <code>TaskDialog<\/code>\ncontrol<\/a>,\na series of <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-in-windows-forms-runtime-in-net-5-0\/#listview-enhancements\"><code>ListView<\/code> control\nenhancements<\/a>,\n<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-in-windows-forms-runtime-in-net-5-0\/#filedialog-enhancement\"><code>FileDialog<\/code> class\nenhancements<\/a>\nand a noticeable <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/whats-new-in-windows-forms-runtime-in-net-5-0\/#performance-improvements\">performance and memory usage improvement around\nGDI+<\/a>.\n.NET (in .NET Core 3.1) also changed the default font for WinForms Forms\nand UserControls to Segoe UI, 9pt to let the typical WinForms UI have a more\nmodern look and feel.<\/p>\n<\/li>\n<li>\n<p>.NET 6 built on that, and introduced the option to set the default font for\nthe whole WinForms Application to what ever font you like. In the VB\nApplication Framework, this is done now by an additional Application Event\nwhich we introduced in the .NET 6 time frame called\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.applyapplicationdefaults?view=windowsdesktop-7.0\"><code>ApplyApplicationDefaults<\/code><\/a>. This event allows to set values for the\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.applyapplicationdefaultseventargs.highdpimode?view=windowsdesktop-7.0#microsoft-visualbasic-applicationservices-applyapplicationdefaultseventargs-highdpimode\">HighDpiMode<\/a>,\nthe <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.applyapplicationdefaultseventargs.font?view=windowsdesktop-7.0#microsoft-visualbasic-applicationservices-applyapplicationdefaultseventargs-font\">application&#8217;s default\nfont<\/a>\nand the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.applyapplicationdefaultseventargs.minimumsplashscreendisplaytime?view=windowsdesktop-7.0#microsoft-visualbasic-applicationservices-applyapplicationdefaultseventargs-minimumsplashscreendisplaytime\">minimum splash dialog display\ntime<\/a>.\nFor more about the Application Events in the VB Application Framework see below.<\/p>\n<\/li>\n<li>\n<p>.NET 7 introduces <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/winforms-cross-platform-dotnet-maui-command-binding\/\">Command Binding for\nWinForms<\/a>,\nwhich makes it easier to apply a UI Controller architecture based on the MVVM\npattern for WinForms Apps, and introduce Unit Tests to WinForms App. General\nimprovements in WinForms in addition to that were <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/winforms-enhancements-in-dotnet-7\/#high-dpi-and-scaling-improvements\">rendering in HighDPI per\nMonitor V2\nscenarios<\/a>\nand &#8211; counting for all versions from .NET 5 to .NET 7 &#8211; <a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/winforms-enhancements-in-dotnet-7\/#accessibility-improvements-and-fixes\">repeatedly improved\naccessibility\nsupport<\/a>\nwith Microsoft UI Automation pattern work better with accessibility tools like\nNarrator, Jaws and NVDA.<\/p>\n<\/li>\n<\/ul>\n<h2>The new Visual Basic Application Framework Experience<\/h2>\n<p>In contrast to the project property Application Framework Designer experience in\nearlier versions of Visual Studio and for .NET Framework, you will noticed that\nthe project properties UI in Visual Studio has changed. It&#8217;s style is now in\nparity with the project properties experience for other .NET project types: we\nhave invested into modernizing the experience for developers, focusing on\nenhancing productivity and a modern look and feel.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2023\/03\/AppFrameworkProperties.png\" alt=\"Screenshot of the new Visual Basic Application Framework project settings designer.\" \/><\/p>\n<p>We&#8217;ve added theming and search to the new experience. If this is your first time\nyou&#8217;re working with the new project properties experience in Visual Studio, it&#8217;s\na good idea to read up on the <a href=\"https:\/\/devblogs.microsoft.com\/visualstudio\/revamped-project-properties-ui\/\">the introductory\nblog<\/a>.<\/p>\n<p>In contrast to C# projects, Visual Basic Application Framework projects use a\nspecial file for storing the Application Framework project settings: the\n<em>Application.myapp<\/em> file. We&#8217;ll talk more about the technical details of how\nthis file connects the project settings to the VB project specific code\ngeneration of the <code>My<\/code> namespace later, but one thing to keep in mind is how the\nUI translates each property&#8217;s value to this file:<\/p>\n<ul>\n<li>\n<p><strong>Windows Visual Styles<\/strong> is to determine if the application will use the most\ncurrent version for the Control Library <em>comctl.dll<\/em> to provide control\nrendering with modern visual styling. This setting translates to the value\n<code>EnableVisualStyles<\/code> of type <code>Boolean<\/code> inside of <em>Application.myapp<\/em>.<\/p>\n<\/li>\n<li>\n<p><strong>Single-instance application<\/strong> is to determine if the application will prevent\nusers from running multiple instances of the application. This setting is\nswitched off by default, which allows multiple instances of the application to\nbe run concurrently. This setting translates to the value <code>SingleInstance<\/code> of\ntype <code>Boolean<\/code>.<\/p>\n<\/li>\n<li>\n<p><strong>Save user settings on exit<\/strong> is to determine if the application settings are\nautomatically saved when an app is about to shut down. The settings can be\nchanged with the settings editor. In contrast to .NET Framework, a new Visual\nBasic Application Framework App doesn&#8217;t contain a settings file by default,\nbut you can easily insert one over the project properties, should you need\none, and then <a href=\"https:\/\/learn.microsoft.com\/visualstudio\/ide\/managing-application-settings-dotnet?view=vs-2022\">manage the settings\ninteractively<\/a>.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2023\/03\/InsertNewSettings.png\" alt=\"Screenshot of the Settings section in the Application Framework project&#039;s property pages\" \/><\/p>\n<p>Adding to the list of settings automatically generates respective code, which\ncan be easily access over the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/visual-basic\/language-reference\/objects\/my-settings-object\"><code>My<\/code> object in the Visual Basic Application\nFramework at\nruntime<\/a>.\nThis settings translates to the value <code>SaveMySettingsOnExit<\/code> of type\n<code>Boolean<\/code>.<\/p>\n<\/li>\n<li>\n<p><strong>High DPI mode<\/strong> is to identify the application-wide HighDpiMode for the\napplication. Note that this setting can be programmatically overridden through\nthe <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.applyapplicationdefaultseventargs.highdpimode?view=windowsdesktop-7.0\"><code>HighDpiMode<\/code>\nproperty<\/a>\nof the <code>ApplyApplicationDefaultsEventArgs<\/code> of the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.applyapplicationdefaults?view=windowsdesktop-7.0\"><code>ApplyApplicationDefaults<\/code><\/a>\napplication event. Choose from the following setting:<\/p>\n<ul>\n<li><strong>DPI unaware (0):<\/strong> The application window does not scale for DPI changes and\nalways assumes a scale factor of 100%. For higher resolutions, this will\nmake text and fine drawings more blurry, but may impose the best setting for\nsome apps which demand a high backwards compatibility in rendering content.<\/li>\n<li><strong>DPI unaware GDI scaled (4):<\/strong> similar to DPI unaware, but improves the\nquality of GDI\/GDI+ based on content. Please note that this mode will <em>not<\/em>\nwork as expected, when you have enabled <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.windows.forms.control.doublebuffered?view=windowsdesktop-7.0\">double\nbuffering<\/a>\nfor control rendering via <a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.windows.forms.control.onpaint?view=windowsdesktop-7.0\"><code>OnPaint<\/code><\/a> and related functionality.<\/li>\n<li><strong>Per monitor (2):<\/strong> Per-Monitor DPI allows individual displays to have their\nown DPI scaling setting. WinForms doesn&#8217;t optimize for this mode, and\nPer-Monitor V2 should be used instead.<\/li>\n<li><strong>Per monitor V2 (3):<\/strong> Per-Monitor V2 offers more advanced scaling features\nsuch as improved support for mixed DPI environments, improved display\nenumeration, and support for dynamically scaling on-client area of windows.\nIn WinForms common controls are optimized for this high dpi mode. Please\nnote the events\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.windows.forms.form.dpichanged?view=windowsdesktop-7.0\">Form.DpiChange<\/a>,\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.windows.forms.control.dpichangedafterparent?view=windowsdesktop-7.0\">Control.DpiChangedAfterParent<\/a>\nand\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/system.windows.forms.control.dpichangedbeforeparent?view=windowsdesktop-7.0\">Control.DpiChangeBeforeParent<\/a>,\nwhen your app need to scale up or down content based on a changed DPI\nenvironment, for example, when the user of your app has dragged a Form from\none monitor to another monitor with a different DPI setting.<\/li>\n<li><strong>System aware (1):<\/strong> The application queries for the DPI of the primary\nmonitor once and uses this for the application on all monitors. When content\nin Forms is dragged from one monitor to another with a different HighDPI\nsetting, content might become blurry. SystemAware is WinForm&#8217;s most\ncompatible high-dpi rendering mode for all supported controls.<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Authentication mode<\/strong> is to specify the method of identifying the logged-on\nuser, when needed. The setting translates to the value <code>AuthenticationMode<\/code> as\nan enum value of type <code>Integer<\/code>:<\/p>\n<ul>\n<li>0: The <code>WindowsFormsApplicationBase(AuthenticationMode)<\/code> constructor does\nnot automatically initialize the principal for the application&#8217;s main\nthread. It&#8217;s completely the developer&#8217;s task, to manage authentication for\nthe user.<\/li>\n<li>1: The <code>WindowsFormsApplicationBase(AuthenticationMode)<\/code> constructor\ninitializes the principal for the application&#8217;s main thread with the current\nuser&#8217;s Windows user info.<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Shutdown mode<\/strong> is to to indicate which condition causes the application to\nshut down. This setting translates to the value <code>ShutdownMode<\/code> as an enum\nvalue of type <code>Integer<\/code> (Note: Please also refer to the application event\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.shutdown?view=windowsdesktop-7.0\">ShutDown<\/a>\nand the further remarks down below.):<\/p>\n<ul>\n<li>0: When the main form closes.<\/li>\n<li>1: Only after the last form closes.<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Splash screen<\/strong> represents the name of the form to be used as a splash screen\nfor the application. Note that the file name does not need to include the\nextension (.vb). This setting translates to the value <code>SplashScreen<\/code> of type\n<code>String<\/code>.<\/p>\n<blockquote>\n<p><strong>Note:<\/strong> you will may be missing the settings for the Splash dialog up to\nVisual Studio 2022 version 17.5. For a workaround, read the comments in\nthe section &#8220;A look behind the scenes&#8221;. To recap: a &#8220;Splash&#8221; dialog is\ntypically displayed for a few seconds when an application is launched.\nVisual Basic has an item template which you can use to add a basic splash\ndialog to your project. It usually displays the logo or name of the\napplication, along with some kind of animation or visual effects, to give\nusers the impression that the application is loading or initializing. The\nterm &#8220;splash&#8221; in this context is used because the dialog is designed to\ncreate a splash or impact on the user, drawing their attention to the\napplication while it loads.<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p><strong>Application Framework<\/strong> is saved both in the <em>Application.myapp<\/em> file and the\n<em>.vbproj<\/em> file:<\/p>\n<ul>\n<li><em>Application.myapp<\/em> saves the setting <code>MySubMain<\/code> of type <code>Boolean<\/code> to\nidentify if the Application Framework is enabled.<\/li>\n<li><em>.vbproj<\/em> uses the setting <code>MyType<\/code> for identifying the usage of the\nApplication Framework for a VB project. If the Application Framework is\nenabled, the value is <em>WindowsForms<\/em>; if the Application Framework is\ndisabled, the value is <em>WindowsFormsWithCustomSubMain<\/em>.<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>Startup object<\/strong> is the name of the form that will be used as the entry\npoint, without its filename extension. Note: this property is found in the\nproject property Settings under the <em>General<\/em> section, and not in the\nApplication Framework section. This setting translates to the value <code>MainForm<\/code> of type\n<code>String<\/code>, when the Application Framework is activated. The start object setting in\nthe <em>.vbproj<\/em> file is ignored in that case &#8211; see also the comments below on this\ntopic.<\/p>\n<\/li>\n<\/ul>\n<h3>Custom constants new look<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2023\/03\/custom-constants.png\" alt=\"Screenshot of the new custom constants editor in the project properties UI.\" \/><\/p>\n<p>We are introducing a new custom constants-control in the modernized Project\nProperty Pages for VB Projects, that allows to encode the input to the format\nkey=&#8221;value&#8221;. Our goal is that users will be able to input their custom constants\nin a more streamlined key-value pair format, thus enhancing their productivity.\nFeedback is welcomed &#8211; if you have any comments or suggestions, feel free to\nreach out to the <a href=\"https:\/\/github.com\/dotnet\/project-system\/\">project system\nteam<\/a> by filing a new issue or\ncomment on existing ones.<\/p>\n<h2>A look behind the scenes of the WinForms VB Application Framework<\/h2>\n<p>The way basic properties and behaviors of a WinForms app are controlled and configured is fundamentally different between C# and Visual Basic. In C#, every app\nstarts with a static method called <code>main<\/code> which can usually be found in a file\ncalled <em>Program.cs<\/em>, and in that <code>main<\/code> method all the setting get applied.<\/p>\n<p>That is different in Visual Basic. Since VB Apps in WinForms are based on the\nApplication Framework runtime, there are a few features, which aren&#8217;t\nintrinsically available to C# WinForms apps to begin with, like configuring to\nautomatically show Splash dialogs (see below) or ensure a single instance\napplication start. Since you configure most of the parts of your app\ninteractively in VB with the settings described above at design time, the actual\ncode which honors or ensures those settings later at runtime is mostly\ncode-generated and somewhat hidden behind the scenes. The starting point of a VB\napp is therefore not so obvious. There are also a series of differences in .NET\nVisual Basic apps when it comes to hooking up event code which is supposed to\nrun, for example when a VB WinForms app starts, ends, or runs into an unhandled\nexception &#8211; just to name a few examples.<\/p>\n<p>That all said, technically Visual Basic doesn&#8217;t break any fundamental rules.\nUnder the hood, there is of course a <code>Shared Sub Main<\/code> when you activate the\nApplication Framework. You just do not write it yourself, and you don&#8217;t see it,\nbecause it is generated by the VB compiler and then automatically added to your\nStart Form. This is done by activating the VB compiler switch\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/visual-basic\/reference\/command-line-compiler\/main\"><code>\/main<\/code><\/a>.<\/p>\n<p>At the same time, when you are activating the Application Framework, a series of\nconditional compiler constants are defined. One of the constants is called\n<code>_mytype<\/code>. If that constant is defined as <code>Windows<\/code> then the VB compiler\ngenerates all the necessary infrastructure code to support the Application\nFramework. If that constant is defined as <code>WindowsFormsWithCustomSubMain<\/code>\nhowever, the VB compiler just generates the bare minimum infrastructure code and\ndoesn&#8217;t apply any settings to the WinForms app on startup. The latter happens,\nwhen you deactivate the Application Framework. This setting is stored in the\n<em>vbproj<\/em> project file, along with the Start Form. What&#8217;s important to know\nthough in this context: only in the case of <code>WindowsFormsWithCustomSubMain<\/code>, so\nwith the Application Framework <em>deactivated<\/em>, is the Start Form definition\nactually taken from the <em>vbproj<\/em> file. When the Application Framework <em>is<\/em>\nactivated however then that is the case when the aforementioned\n<em>Application.myapp<\/em> file is used as the settings container. Note, that by\ndefault you cannot find that file in the solution explorer.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2023\/03\/myAppInSolutionExplorer.png\" alt=\"Screenshot of solution explorer showing the Application.myapp file.\" \/><\/p>\n<p>You need to make sure first to show <em>all files<\/em> for that project (see screenshot\nabove). Then you can open the <em>My Project<\/em>-folder and show that setting file in\nthe editor by double-clicking it in the solution explorer. The content of that\nfile looks something like this:<\/p>\n<pre><code class=\"language-xml\">&lt;?xml version=\"1.0\" encoding=\"utf-16\"?&gt;\r\n&lt;MyApplicationData xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\"&gt;\r\n  &lt;MySubMain&gt;true&lt;\/MySubMain&gt;\r\n  &lt;MainForm&gt;Form1&lt;\/MainForm&gt;\r\n  &lt;SingleInstance&gt;false&lt;\/SingleInstance&gt;\r\n  &lt;ShutdownMode&gt;0&lt;\/ShutdownMode&gt;\r\n  &lt;EnableVisualStyles&gt;true&lt;\/EnableVisualStyles&gt;\r\n  &lt;AuthenticationMode&gt;0&lt;\/AuthenticationMode&gt;\r\n  &lt;SaveMySettingsOnExit&gt;true&lt;\/SaveMySettingsOnExit&gt;\r\n  &lt;HighDpiMode&gt;3&lt;\/HighDpiMode&gt;\r\n&lt;\/MyApplicationData&gt;<\/code><\/pre>\n<p><strong>Note:<\/strong> Visual Studio 2022 before version 17.6 (Preview 3) won&#8217;t have the\noption to pick a Splash Dialog interactively, as mentioned above. We will have\nan interactive designer for setting the splash form only from that version on\non. Up to then, you can manually patch the <em>Application.myapp<\/em> file to trigger\nthe code generation for the Splash dialog. Insert the following line of code in\nthat file and save the changes.<\/p>\n<pre><code class=\"language-xml\">&lt;SplashScreen&gt;SplashDialog&lt;\/SplashScreen&gt;<\/code><\/pre>\n<p>When you do this, make sure <em>not<\/em> to include the filename extension (.vb) in\nthat definition, because otherwise the required code does not get generated.<\/p>\n<h3>Application.myapp as the source for code generation<\/h3>\n<p>Now, if you take a closer look at that file&#8217;s properties in the property\nbrowser, you&#8217;ll see that it is triggering a custom tool which is invoked\nwhenever that file is saved. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2023\/03\/myAppCustomTool.png\" alt=\"Screenshot of solution explorer showing the properties for the Application.myapp file.\" \/><\/p>\n<p>And that custom tool generates VB code which you\ncan find under the <em>Application.myapp<\/em> node in the Solution Explorer in\n<em>Application.Designer.vb<\/em>. It does the following:<\/p>\n<ul>\n<li>It defines a <code>Friend Partial Class MyApplication<\/code>. With the Application\nFramework enabled, that class is inherited from\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase?view=windowsdesktop-7.0\"><code>WindowsFormsApplicationBase<\/code><\/a>.\nYou don&#8217;t see that <code>Inherits<\/code> statement here and the reason is that <a href=\"https:\/\/github.com\/dotnet\/roslyn\/blob\/main\/src\/Compilers\/VisualBasic\/Portable\/Symbols\/EmbeddedSymbols\/VbMyTemplateText.vb\">the major\npart of that Class&#8217;\ndefinition<\/a>\nis injected by the Visual Basic compiler based on the earlier defined\nconditional constant <code>_myapp<\/code>.<\/li>\n<li>It generates the code to apply all the settings which were saved in\n<em>Application.myapp<\/em> file.<\/li>\n<li>It creates code for a method which overrides\n<a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.oncreatemainform?view=windowsdesktop-7.0\"><code>OnCreateMainForm<\/code><\/a>.\nIn that method, it assigns the Form, which is defined as the start form in the\n<em>Application.myapp<\/em> file.<\/li>\n<\/ul>\n<blockquote>\n<p><strong>Warning<\/strong>: The <em>Application.Designer.vb<\/em> is not supposed to be edited, as it&#8217;s\nauto-generated. Any changes will be lost as soon as you make changes to <em>Application.myapp<\/em>. Instead, use the project properties UI.<\/p>\n<\/blockquote>\n<p>Now, the class which is injected by the compiler is also responsible for\ngenerating everything which the Visual Basic Application Framework provides you\nvia the <code>My<\/code> namespace. The <code>My<\/code> namespace simplifies access to frequently used\ninformation about your WinForms app, your system, or simplifies access to\nfrequently used APIs. Part of the <code>My<\/code> namespace for an activated Application\nFramework is the <code>Application<\/code> property, and its return type is of exactly that\ntype which is defined by the class generated based on your Application Settings\nand then merged with the injected Visual Basic compiler file mentioned earlier.\nSo, if you access <code>My.Application<\/code> you are basically accessing a single instance\nof the <code>My.MyApplication<\/code> type which the generated code defines.<\/p>\n<p>With this context understood, we can move on to how two additional features of\nthe Application Framework work and can be approached. The first one is extending\nthe <code>My<\/code> namespace with additional function areas. We won&#8217;t go too much into\nthem, because there are <a href=\"https:\/\/learn.microsoft.com\/dotnet\/visual-basic\/developing-apps\/customizing-extending-my\/extending-the-my-namespace\">detailed docs about the <code>My<\/code> namespace and how to\nextend\nit<\/a>.<\/p>\n<p>An even more important concept to understand are the Application Events which are\nprovided by the Application Framework. Since there isn&#8217;t a good way to intercept\nthe startup or shut down of an app (since that code gets generated\nand sort of hidden inside the main Form) Application Events are the way to be\nnotified of certain application-global occurrences.  <\/p>\n<p>Note in this context, that there is a small breaking change in the UI: while in\n.NET Framework, you had to insert a code file named <em>ApplicationEvents.vb<\/em> via\nthe Property Settings of the VB project, in a .NET Core App this file will be\nthere from the start when you&#8217;ve created a new Application Framework project.<\/p>\n<p>To wire up the available Application events, you open that <em>ApplicationEvent.vb<\/em>\ncode file, and then you select ApplicationEvents from the Object drop-down list,\nand the application event you want to write up from the events list:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2023\/03\/WireUpAppEvents.gif\" alt=\"Animated gif showing how to wire app Application Events in the ApplicationEvent.vb code file\" \/><\/p>\n<p>As you can see, the <em>ApplicationEvent.vb<\/em> code file again extends the <code>MyApplication<\/code> class &#8211; this time by the events handler you place there on demand. The options you have here are:<\/p>\n<ul>\n<li><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.startup?view=windowsdesktop-7.0\"><strong><code>Startup<\/code><\/strong><\/a>: raised when the application starts, before the start form is created.<\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.shutdown?view=windowsdesktop-7.0\"><strong><code>Shutdown<\/code><\/strong><\/a>: raised after all application forms are closed.  This event is not raised if the application terminates abnormally.<\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.unhandledexception?view=windowsdesktop-7.0\"><strong><code>UnhandledException<\/code><\/strong><\/a>: raised if the application encounters an unhandled exception.<\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.startupnextinstance?view=windowsdesktop-7.0\"><strong><code>StartupNextInstance<\/code><\/strong><\/a>: raised when launching a single-instance application and the application is already active.<\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.networkavailabilitychanged?view=windowsdesktop-7.0\"><strong><code>NetworkAvailabilityChanged<\/code><\/strong><\/a>: raised when the network connection is connected or disconnected.<\/li>\n<li><a href=\"https:\/\/learn.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.applyapplicationdefaults?view=windowsdesktop-7.0\"><strong><code>ApplyApplicationDefaults<\/code><\/strong><\/a>: raised when the application queries default values to be set for the application.<\/li>\n<\/ul>\n<p><strong>Note:<\/strong> More general information about the <a href=\"https:\/\/learn.microsoft.com\/dotnet\/visual-basic\/developing-apps\/development-with-my\/overview-of-the-visual-basic-application-model\">Visual Basic Application Model<\/a> is provided through the Microsoft Learn Docs about this topic. Also note, that, on top of the extensibility of the <code>My<\/code> namespace, this <a href=\"https:\/\/learn.microsoft.com\/dotnet\/visual-basic\/developing-apps\/customizing-extending-my\/extending-the-visual-basic-application-model\">Application Model also has extensibility points<\/a> which are also described in great detail by the respective docs.<\/p>\n<h2>Summary<\/h2>\n<p>With the new and modernized project properties pages, WinForm&#8217;s Application\nFramework is ready for new, .NET 6,7,8+ based Visual Basic Apps to develop. It&#8217;s\nalso the right time to think about modernizing your older .NET Framework based\nVB Apps and bring them over to .NET 6,7,8+. WinForms and the .NET runtime\ndeliver countless new features and provide considerable performance improvements\nfor your apps in almost every area. Visual Basic and the Visual Basic\nApplication Framework are and continue to be first class citizens and are fully\nsupported in WinForms. Our plans are to continue modernizing around the VB App\nFramework in the future without breaking code for existing projects.<\/p>\n<p>And, as always: Feedback about the subject matter is really important to us, so\nplease let us know your thoughts and additional ideas! Please also note that the\nWinForms .NET and the <a href=\"https:\/\/github.com\/dotnet\/winforms\/tree\/main\/src\/Microsoft.VisualBasic.Forms\/src\/Microsoft\/VisualBasic\/ApplicationServices\">Visual Basic Application Framework runtime<\/a> is open source,\nand you can contribute! If you have general feature ideas, encountered bugs, or\neven want to take on existing issues around the WinForms runtime and submit PRs,\nhave a look at the <a href=\"https:\/\/github.com\/dotnet\/winforms\">WinForms Github repo<\/a>.\nIf you have suggestions around the WinForms Designer feel free to file new\nissues there as well.<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since .NET 6, we updated the WinForms runtime to support and improve the Visual Basic Application Framework. In Visual Studio 2022, we also modernized the related user experience. Time to take a closer look how all this works behind the scenes, lets you move from .NET Framework to .NET 6\/7+ and provides a great opportunity to modernize your WinForms Visual Basic Apps!<\/p>\n","protected":false},"author":9483,"featured_media":44696,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,7199,7163],"tags":[],"class_list":["post-44695","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-visual-basic","category-winforms"],"acf":[],"blog_post_summary":"<p>Since .NET 6, we updated the WinForms runtime to support and improve the Visual Basic Application Framework. In Visual Studio 2022, we also modernized the related user experience. Time to take a closer look how all this works behind the scenes, lets you move from .NET Framework to .NET 6\/7+ and provides a great opportunity to modernize your WinForms Visual Basic Apps!<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/44695","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/users\/9483"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/comments?post=44695"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/44695\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/44696"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=44695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=44695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=44695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}