ARTICLE AD BOX
This is because you don't reference the appropriate assemby, System.Windows.Forms. Referencing assemblies has nothing to do with using directive.
The answer depends on the platform.
.NET Framework
If you are using .NET Frameworks (versions prior to v.5), your project file should have
<ItemGroup> <Reference Include="System.Windows.Forms" /> <!-- Very usually: --> <Reference Include="System.Drawing" /> </ItemGroup>Here, you have to reference GAC (Global Assembly Cache) assemblies individually, depending on what you want to use.
.NET
.NET (v.5 and later) requires the properties
<PropertyGroup> <TargetFramework>$(TargetFramework)-windows</TargetFramework> <UseWindowsForms>true</UseWindowsForms> </PropertyGroup>Here, I assume that you preliminary define one of the target frameworks (TFM) as the property TargetFramework, such as net5.0, net10.0, or whatever you want. This framework version should be installed with your .NET installation.
The list of the valid target framework values can be found here. It covers all the available framework versions, including netcoreapp, netstandard, etc.
This way, if the resulting property TargetFramework for console applications is net5.0, net6.0, ... net10.0, for WPF or System.Windows.Forms applications it should become net5.0-windows, net6.0-windows, ... net10.0-windows. In this case, it makes all the required Windows-related assemblies referenced automatically.
Generally, the targets frameworks of this kind, with added OS name like -windows, are called OS-specific TFMs. Please see the section OS-specific TFMs on the same documentation page.
Target frameworks can be specified in a project file using, alternatively, the property <TargetFramework> or <TargetFrameworks>. For further details, please see the section How to specify a target framework on the same documentation page.
Note that all these properties can be defined either in .csproj project files, or in the file(s) named Directory.Build.props. This way, the defined properties will cover all the project files in the current or nested source code directories.
Using
Additionally, you have to learn about the using directive.
It should not be confused with using statement related to the use of disposable objects.
The using directive is merely a way to use a shortened syntax for fully-qualified names.
For example, you can use the class System.Windows.Forms.Form. C# needs a fully-qualified name to recognize the type you mean. If you use this name more than once, you can shorted your code by reusing a part of the name:
using System.Windows.Forms; //... Form myForm = new(); // implied System.Windows.Forms.Formor
using Form = System.Windows.Forms.Form; // alias directive //... Form myForm = new(); // implied System.Windows.Forms.FormBut before you can use some definition in a fully-qualified or shortened form, you need it to be defined. If it is defined in some other assembly, GAC or not, you have to reference this assembly.
