5

I apologize ahead of time for my lack of terminology and understanding, I am very new to .NET and Visual Studio.

I am on Windows 7 64 Bit in Visual Studio Community 2019 Version 16.10.4 making a Console Application for .NET 5.0 using Visual Basic.

I am trying to use the Windows.Forms.Form object and make a little window pop-up and ask the user to select an item from a ListBox. But so far I have not been successful in adding System.Windows.Forms to my project.

I tried Imports System.Windows.Forms but that gave the warning "Namespace or type specified in the Imports 'System.Windows.Forms' doesn't contain any public member or cannot be found.".

I went to Project -> Add COM Reference -> "System_Windows_Forms" (Version 2.0)

That reference has a warning: "Failed to create the wrapper assembly for type library {215d64d2-031c-33c7-96e3-61794cd1ee61}. Type library 'System_Windows_Forms' was exported from a CLR assembly and cannot be re-imported as a CLR assembly."

I heard that adding the .dll directly can fix that, so I removed the COM reference and went to Project -> Add Project Reference -> Browse -> "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Windows.Forms.dll"

When I searched for Forms.dll I found about 10 files so I just picked the one that looked most correct. After that, I could use the objects in my script and Visual Studio could autocomplete the properties and functions of the objects. But when I actually went into the debugger and tried to run the script, I get the error:

System.IO.FileNotFoundException
HResult=0x80070002
Message=Could not load file or assembly 'System.Drawing.Common, Version=0.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'. The system cannot find the file specified.
Source=System.Windows.Forms
StackTrace:
at System.Windows.Forms.Control..ctor(Boolean autoInstallSyncContext)
at System.Windows.Forms.Control..ctor()
at System.Windows.Forms.ListControl..ctor()
at System.Windows.Forms.ListBox..ctor()
at GetNovel.Program.DetermineNovel()

Here is my sub that causes the error:

Sub DetermineNovel()
    Dim Novels As Object = {{"Moon's Labyrinth", "ML", True, 262, 362}, {"Keyboard Immortal", "KI", False, 254, 254}}

    Dim SelectBox As New Windows.Forms.ListBox
    Dim i As Long
    For i = LBound(Novels) To UBound(Novels)
        SelectBox.Items.Add(Novels(i, 0))
    Next i

    Dim NovelSelect As New Windows.Forms.Form
    NovelSelect.Controls.Add(SelectBox)
    NovelSelect.Text = "Select Novel:"

    NovelSelect.Show()
End Sub
EKOlog
  • 410
  • 7
  • 19
Toddleson
  • 4,321
  • 1
  • 6
  • 26
  • 2
    Post your Project configuration file (you probably have `Exe` but you didn't add `true`) in there. – Jimi Jul 23 '21 at 23:23
  • 1
    Make life easy; create a windows forms project. You can run a windows forms app from the console – Caius Jard Jul 24 '21 at 08:10
  • I believe you need to install it form Nuget. – Usama Aziz Jul 24 '21 at 10:24
  • Maybe you need to know how to [create a new WinForms app in .NET5](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/get-started/create-app-visual-studio?view=netdesktop-5.0) – Xingyu Zhao Jul 26 '21 at 01:46
  • @CaiusJard & XingyuZhao Sorry for taking so long to reply. Thank you for your advice, I will try creating a Windows Forms Project. – Toddleson Jul 26 '21 at 13:54
  • Bear in mind that there are various issues with the windows forms designer in .net core/.net 5 and some functionality is missing – Caius Jard Jul 26 '21 at 14:14

1 Answers1

12

Manually edit the .csproj file. Set the TargetFramework and UseWindowsForms as shown.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

  ...

Edit: Updated to .NET 6

Paul Williams
  • 3,099
  • 38
  • 34