{"id":30796,"date":"2020-11-23T09:15:59","date_gmt":"2020-11-23T16:15:59","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/dotnet\/?p=30796"},"modified":"2020-11-23T09:15:59","modified_gmt":"2020-11-23T16:15:59","slug":"visual-basic-winforms-apps-in-net-5-and-visual-studio-16-8","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/dotnet\/visual-basic-winforms-apps-in-net-5-and-visual-studio-16-8\/","title":{"rendered":"Visual Basic WinForms Apps in .NET\u00a05\u00a0and\u00a0Visual\u00a0Studio\u00a016.8"},"content":{"rendered":"<p>Visual Basic, along with its Application Framework, is supported in .NET 5 and Visual Studio 16.8!\nVisual Studio 16.8 includes the Windows Forms Designer,\nso Visual Basic is ready for you to migrate existing applications or create new applications.<\/p>\n<p>While .NET Core has had Visual Basic since the first release, and WinForms since it was released in .NET Core 3.1, it did not include the Application Framework library and WinForms Designer support Visual Basic programmers expect. .NET 5 is the next version of .NET Core and we think .NET 5 is ready for you to migrate existing .NET Framework apps or create new WinForms applications.<\/p>\n<p>We\u2019re excited about the updates for Visual Basic in .NET 5, but first\na reminder that you can base your decision to migrate to .NET 5 on whether or not you want its new features\nand how you need to interop with .NET 5 applications. The .NET Framework will\nbe supported as part of Windows for a very long time.\nNew features will not be added to .NET Framework. New features, including new WinForms features,\nwill be added only to .NET 5 and future versions.<\/p>\n<p>Visual Studio 16.8 and .NET 5 include the following updates for WinForms\ndevelopment in .NET 5:<\/p>\n<ul>\n<li><strong>Designer Event Handling support:<\/strong> Visual Basic events are hooked up with\na <em>Handles<\/em> keyword on the code behind method. This presented significant\nchallenges to the WinForms designer and did not work correctly in .NET Core\n3.1. This has been fixed.<\/li>\n<li><strong>Application Framework:<\/strong> The Visual Basic Application Framework provides\nevent-based startup, meaning you don\u2019t need a <code>Sub Main<\/code> for WinForms\napplications. A new version of the Application Framework is available in\n.NET 5.<\/li>\n<li><strong>Single Instance applications:<\/strong> .NET 5 introduces Single Instance\nApplications to the .NET Core family. The new version also has updated logic\nthat should work well in more scenarios.<\/li>\n<li><strong>Updates to Visual Studio dialogs:<\/strong> Updates have been made to a few\ndialogs to support Visual Basic features in the new project format. This\nwork is ongoing.<\/li>\n<li><strong>Visual Basic WinForms template<\/strong>: We waited to add the template to Visual\nStudio until you could create WinForms applications with the Application\nFramework. This new template is the default in Visual Studio, and available via a switch on <code>dotnet new<\/code> in the .NET CLI.<\/li>\n<li><strong>Additional Visual Basic Runtime features:<\/strong> The last few versions of .NET\nCore have ported features of the Visual Basic Runtime. We believe this work\nis complete and that missing features can\u2019t work the same way on .NET 5.<\/li>\n<\/ul>\n<h3>Creating .NET 5 Application Framework WinForms Apps in Visual Studio<\/h3>\n<p>You\u2019ll find the new .NET 5 template in the Create New Project dialog. Naming has\nbeen updated for templates, and you\u2019ll find the .NET Framework templates names\ninclude \u201cFramework\u201d and the templates that simply indicate .NET are for .NET 5.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2020\/11\/01_NewVbProject.png\" alt=\"Creating a new Visual Basic .NET 5 App with Application Framework support\" \/><\/p>\n<p>Many aspects of a WinForms Application Framework App\u2019s behavior are controlled\nthrough settings in the Project Property Designer, which is shown by selecting\n<em>Properties<\/em> in the context menu of a VB Project in the Solution Explorer:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2020\/11\/02_ApplicationFrameworkDesigner.png\" alt=\"The different settings of the WinForms Visual Basic Application Framework Designer\" \/><\/p>\n<p><strong>Target Framework:<\/strong> The Visual Basic Application Framework is not supported in\n.NET Core 3.1 or earlier versions of .NET Core. As a result, if you switch the\nTarget Framework in the Application Designer to .NET Core 3.1, an Application\nFramework based project will no longer compile. (See also sections \u201cCreating\n.NET Core 3.1 or .NET 5 Apps at the Command Line\u201d and \u201cKnown Issues\u201d.)<\/p>\n<p><strong>Enable Application Framework:<\/strong> The Application Framework is enabled by\ndefault. When it is enabled, it provides easy one-click configuration settings\nand a simplified access to the coded application events (see below).<\/p>\n<p><strong>Enable XP visual styles:<\/strong> This option enables the OS-controlled themed\nrendering of controls, which was first introduced in Windows XP and retained the\nname going forward.<\/p>\n<p><strong>Make single instance application:<\/strong> If this option is clicked, the Application\nFramework ensures, that a WinForms App is only started once. (Tip: If this\noption is not used, the <a href=\"https:\/\/docs.microsoft.com\/dotnet\/api\/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.startupnextinstance?view=net-5.0\">StartUpNextInstance application\nevent<\/a>\nlets you control whether another instance of the WinForms app can be started).<\/p>\n<p><strong>Save My Settings on Shutdown:<\/strong> Automatically saves settings properties when\nthe WinForms application is closed. Note that the settings feature is not yet\nfully implemented in the new WinForms Designer for Visual Basic.<\/p>\n<p><strong>Authentication mode:<\/strong> This option controls whether the <em>User<\/em> property\nmirrors the Windows User through the <em>My<\/em>-namespace, or if you want to implement\nthe user management yourself. Please note, that this does not cause any login\ndialog to be displayed or other authentication \u2013 it is exclusively for how the\n<em>User<\/em> property in the <em>My<\/em> namespace works.<\/p>\n<p><strong>Splash screen:<\/strong> Lets you pick a Splash screen form, which gets automatically\ndisplayed, when the WinForms app starts.<\/p>\n<h3>Differences from .NET Framework<\/h3>\n<p>.NET 5 is in the .NET Core family and has differences in the APIs and libraries\nthat are available, the Project System, and how applications are deployed. Check out this\n<a href=\"https:\/\/devblogs.microsoft.com\/dotnet\/porting-desktop-apps-to-net-core\/\">blog post for porting applications to .NET\nCore<\/a>.<\/p>\n<p>In general, your experience in Visual Studio will be similar for .NET 5 and .NET\nFramework. One difference is how you hook up Application Framework events for\nstartup, network availability change detection, shutdown and unhandled exception\nnotification. An empty <em>ApplicationEvents.vb<\/em> file to contain these events is\ncreated by the WinForms template in .NET 5, while in .NET Framework the file is\nnot created until the developer clicks the <em>View Application Events<\/em> button. As\na result, the procedure for wiring applications events in your application is:<\/p>\n<ol>\n<li>Switch to the <em>ApplicationEvents.vb<\/em> code file in the code editor.<\/li>\n<li>From the middle dropdown view in the Code editor, chose <em>(MyApplication\nEvents)<\/em>.<\/li>\n<li>From the right dropdown view in the Code editor, chose the application\nevent, you want to wire up.<\/li>\n<li>This will create the handler where you can add your code.<\/li>\n<\/ol>\n<h3>New Application Framework Event <em>ApplyHighDpiMode<\/em><\/h3>\n<p>Beginning with .NET 5, the Application Framework provides a new application\nevent named <em>ApplyHighDpiMode<\/em>. The default is <em>SystemAware<\/em> which provides the\nsame crisp high dpi rendering at design time and runtime.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2020\/11\/03_NewApplicationEvent.png\" alt=\"Visual Basic's new ApplyHighDpiMode Application Event\" \/><\/p>\n<p>If you want to start your WinForms App with a different high Dpi setting, you can\ndo that easily by handling the <em>ApplyHighDpiMode<\/em> application event:<\/p>\n<ul>\n<li>Open the <em>ApplicationEvent.vb<\/em> code file in the editor.<\/li>\n<li>From the middle dropdown list, chose <em>MyApplication Events<\/em>.<\/li>\n<li>From the right list of events (see screenshot), pick <em>ApplyHighDpiMode<\/em> to\ninsert the event handler stub.<\/li>\n<li>Add the following code (the sample set it to <em>PerMonitorV2<\/em>):<\/li>\n<\/ul>\n<pre><code class=\"language-vb\">\r\nPartial Friend Class MyApplication\r\n    Private Sub MyApplication_ApplyHighDpiMode(\r\n        sender As Object, e As ApplyHighDpiModeEventArgs) Handles Me.ApplyHighDpiMode\r\n    \r\n        e.HighDpiMode = HighDpiMode.PerMonitorV2\r\n\r\n    End Sub\r\nEnd Class\r\n<\/code><\/pre>\n<p>The high DPI mode set in the *ApplyHighDpiMode* event cannot be changed after\nthe first Form has been displayed, and it should be set only once.<\/p>\n<h2>Creating .NET Core 3.1 or .NET 5 VB WinForms Apps with\u00a0the\u00a0CLI<\/h2>\n<p>While we highly recommend Visual Studio and its WinForms Designer, you can also\ncreate applications outside Visual Studio, using the .NET CLI. You need the .NET\n5 SDK, which is installed with Visual Studio, or you can <a href=\"https:\/\/aka.ms\/dotnet-core-download\">download it here<\/a>.<\/p>\n<p>When you create a\nVisual Basic WinForms app at the command line, the Application Framework is NOT\nused by default and you can select either .NET Core 3.1 or .NET 5. Since the\nApplication Framework\u2019s infrastructure was added to the runtime in.NET 5 you\ncannot create Application Framework applications targeting .NET Core 3.1.<\/p>\n<p>With the CLI, you create a new Visual Basic Forms app with the command <code>dotnet\nnew<\/code>:<\/p>\n<pre><code class=\"language-shell\">\r\ndotnet new winforms -o outputDirectoryName --language VB\r\n<\/code><\/pre>\n<p>You control with the command line options how the project should get created:<\/p>\n<p><strong>&#8211;output outputDirectoryName :<\/strong> Determines the VB WinForms project name. A\nsubfolder with this name is automatically created.<\/p>\n<p><strong>&#8211;language vb:<\/strong> Determines that a Visual Basic WinForms project is created.\nIf the <em>&#8211;language<\/em> option is omitted, a C# project is created. If you want\n<code>dotnet new<\/code> to default to Visual Basic, set the environment variable\n<code>DOTNET_NEW_PREFERRED_LANG=VB<\/code>.<\/p>\n<p>By default, <code>dotnet new<\/code> generates a .NET Core 5.0 WinForms Project without the\nApplication Framework, similar to how a C# WinForms project works.\nThe application starts with a <code>Sub Main<\/code> method in module\ncalled <em>Program.vb<\/em> which handles all the necessary set up:<\/p>\n<pre><code class=\"language-vb\">\r\nFriend Module Program\r\n  ' Start and set HighDpiMode, Styles and TextRenderingDefault.\r\n   &lt;STAThread()&gt;\r\n   Friend Sub Main(args As String())\r\n      Application.SetHighDpiMode(HighDpiMode.SystemAware)\r\n      Application.EnableVisualStyles()\r\n      Application.SetCompatibleTextRenderingDefault(False)\r\n      Application.Run(New Form1)\r\n   End Sub\r\nEnd Module\r\n<\/code><\/pre>\n<p>If you want to create a .NET 5 WinForms VB Project with\nApplication Framework support from the command line, then you should use these additional options:<\/p>\n<pre><code class=\"language-shell\">\r\ndotnet new winforms -o VbForms5AF --language VB --framework net5.0 --use-app-framework\r\n<\/code><\/pre>\n<p><strong>&#8211;framework:<\/strong> Determines which .NET version the WinForms App is created. The\nTarget Framework Monikers are<\/p>\n<ul>\n<li><code>netcoreapp3.1<\/code> for .Net Core 3.1 Apps<\/li>\n<li><code>net5.0-windows<\/code> for .NET 5 Apps<\/li>\n<\/ul>\n<p><strong>&#8211;use-app-framework:<\/strong> When this option is present (only possible from .NET\n5.0 and above), a Visual Basic WinForms App based on the Application Framework is\ncreated. The resulting project files are the same as the files created by using\nthe VB .NET 5.0 WinForms template of Visual Studio.<\/p>\n<h2>Changed Project File Format<\/h2>\n<p>.NET Core and .NET 5 applications use a new project file (<em>.vbproj<\/em>) format. The\nnew format is designed to be read and understood by humans as well as MSBuild.\nFiles are no longer listed explicitly. Rather, all files in the folder where the\n<em>.vbproj<\/em> file is saved and any subfolders are <em>implicitly<\/em> part of the project.\nThis is called <em>globbing<\/em>. You\u2019ll see project level imports, project and package\nreference and <em>Option<\/em> settings in this file as well.<\/p>\n<p>The new project file looks like:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2020\/11\/04_VbProjStructure.png\" alt=\"The new Visual Basic .vbproj Project file structure\" \/><\/p>\n<p>Visual Basic project files start with the <code>&lt;Project SDK&gt;<\/code> tag. For WinForms apps\nthis tag needs to be\u2026<\/p>\n<ul>\n<li><code>Microsoft.NET.Sdk<\/code> for .NET 5 (and higher versions) apps and<\/li>\n<li><code>Microsoft.NET.WindowsDesktop<\/code> for .NET Core 3.1 apps.<\/li>\n<\/ul>\n<p>To change the Target Framework in a WinForms App (either Visual Basic, C# or F#),\nchange the <em>Target Framework<\/em> setting by editing the project file.<\/p>\n<p><strong>Important:<\/strong> The Project Properties Designer does not currently support\nchanging between .NET 5 and .NET Core 3.1. And if you inadvertently change it\nthere, your project may not compile. Change the Target Framework manually in the\n<em>.vbproj<\/em> file to the one you need.<\/p>\n<p>Change the <em>TargetFramework<\/em> tag to\u2026<\/p>\n<ul>\n<li><code>netcoreapp3.1<\/code> for .NET 3.1 Apps. Please keep in mind, that 3.1 apps don\u2019t\nsupport the Visual Basic Application Framework.<\/li>\n<li><code>net5.0-windows<\/code> for .NET 5.0 Apps.<\/li>\n<\/ul>\n<h2>Known Issues<\/h2>\n<p>In Visual Studio 16.8, the WinForms Designer still has a few features missing.\nAlso, the project system, which manages the Project Dialog, does not fully\nsupport the Application Framework.<\/p>\n<h3>Selecting the Startup Form in a .NET 5.0 Application Framework App<\/h3>\n<p>In the Project Property Designer, the selection of the <em>Start Object<\/em> is not\ncurrently functional. To work around this issue:<\/p>\n<ul>\n<li>To change the default setting to a different form, make sure that the\nsolution explorer shows all files. If needed, change this setting in the\nSolution Explorer\u2019s toolbar:<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/dotnet\/wp-content\/uploads\/sites\/10\/2020\/11\/05_EditApplicationMyApp.png\" alt=\"Finding the application.myapp file in Solution Explorer\" \/><\/p>\n<ul>\n<li>Expand the branch <em>My Project<\/em> in the solution Explorer.<\/li>\n<li>Click on <em>Application.myapp<\/em> to show it in the code editor. Now, change the\n<em>&lt;MainForm&gt;<\/em> setting to the Form, with which you want the WinForms app to\nstart.<\/li>\n<li>Save the file, to initiate the code generation, that sets the Form you\nselected as the new start form.<\/li>\n<\/ul>\n<h2>Reporting bugs and suggesting features<\/h2>\n<p>Have any comments, suggestions or have you found bugs? Please submit issues\nvia Visual Studio Feedback or as issues in the WinForms GitHub repository. WinForms is\nopen source and developed in the open. Visit\n<a href=\"https:\/\/github.com\/dotnet\/winforms\">https:\/\/github.com\/dotnet\/winforms<\/a>, and get engaged! Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Visual Studio 16.8 and .NET 5.0 for the first time fully support Windows Forms Designer and the VB Application Framework.<\/p>\n","protected":false},"author":9483,"featured_media":30797,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[685,196,7199,7163],"tags":[7198,7200,144,145,319],"class_list":["post-30796","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dotnet","category-dotnet-core","category-visual-basic","category-winforms","tag-net-5-0","tag-application","tag-vb","tag-visual-basic","tag-winforms"],"acf":[],"blog_post_summary":"<p>Visual Studio 16.8 and .NET 5.0 for the first time fully support Windows Forms Designer and the VB Application Framework.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/30796","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=30796"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/posts\/30796\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media\/30797"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/media?parent=30796"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/categories?post=30796"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/dotnet\/wp-json\/wp\/v2\/tags?post=30796"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}