3

According to other answers, I try to add project reference. I open the reference manager to find System.Windows.Forms and add it, but the project reference shows "No items found". So how can I add the reference? enter image description here

enter image description here

4
  • 1
    Looks like you have a console template. Try creating a new project using the WinForms template. Commented Jul 6, 2022 at 14:46
  • 1
    According to your Question Tags, you are specifically using C# 4.0 from 2010? Commented Jul 6, 2022 at 14:48
  • It's .NET-6.0 here, not C# 4.0. I have set a wrong tag. Commented Jul 6, 2022 at 14:59
  • 3
    In Solution Explorer, right-click the Project and select Edit Project File, then add <UseWindowsForms>true</UseWindowsForms> and change, if different, <TargetFramework>net6.0-windows</TargetFramework>. Rebuild. Commented Jul 6, 2022 at 17:19

2 Answers 2

7

As Jimi's comment suggests, you cannot directly reference System.Windows.Forms.dll because it's part of the runtime. Instead, add the following to your project file: <UseWindowsForms>true</UseWindowsForms> and make sure the target framework is set as <TargetFramework>net6.0-windows</TargetFramework> (and not only net6.0), because Windows Forms is only supported on Windows.

Sign up to request clarification or add additional context in comments.

Comments

2

You need change <TargetFramework>net8.0</TargetFramework> to <TargetFramework>net8.0-windows</TargetFramework>, and add <UseWindowsForms>true</UseWindowsForms>

Update:
Following this proposal, I've update my .csproj file from project directory.
New lines in xml element are:

 <PropertyGroup> <TargetFramework>net9.0-windows</TargetFramework> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup> 

That helped me .

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.