I have just started working on c#, and was fiddling with some code sample that I got from some forum.
This code is using a namespace using system.windows.forms
for which I am getting an error:
Forms does not exist in the namespace system.windows.
Also I am getting some error related to undefined functions for senddown
& sendup
which I believe to be in the Forms
name space.
I am using visual studio 10 (with .net frame work 4.0). Any idea how to fix this error?
Expand the project in Solution Tree, Right-Click on References
, Add Reference
, Select System.Windows.Forms
on Framework
tab.
You need to add reference to some non-default assemblies sometimes.
From comments : for people looking for VS 2019+: Now adding project references is Right-Click on Dependencies
in Solution Explorer
.
In case someone runs into this error when trying to reference Windows Forms components in a .NET Core 3+ WPF app (which is actually not uncommon). The solution is to go into the.csproj file (double click it in VS2019) and add it to the property group node containing the target frameworks. Like this:
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<UseWPF>true</UseWPF>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
If you are writing Windows Forms code in a .Net Core app, then it's very probable that you run into this error:
Error CS0234 The type or namespace name 'Forms' does not exist in the namespace 'System.Windows' (are you missing an assembly reference?)
If you are using the Sdk style project file (which is recommended) your *.csproj file should be similar to this:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms>
<RootNamespace>MyAppNamespace</RootNamespace>
<AssemblyName>MyAppName</AssemblyName>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="3.0.0" />
</ItemGroup>
</Project>
Pay extra attention to these lines:
<Project Sdk="Microsoft.NET.Sdk.WindowsDesktop">
<OutputType>WinExe</OutputType>
<UseWindowsForms>true</UseWindowsForms>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="3.0.0" />
Note that if you are using WPF while referencing some WinForms libraries you should add <UseWPF>true</UseWPF>
as well.
Hint: Since .NET 5.0 , Microsoft recommends to refer to SDK Microsoft.Net.Sdk
in lieu of Microsoft.Net.Sdk.WindowsDesktop
.
Quoting Announcing .NET 5.0 :
Windows desktop APIs (including Windows Forms, WPF, and WinRT) will only be available when targeting net5.0-windows . You can specify an operating system version, like net5.0-windows7 or net5.0-windows10.0.17763.0 ( for Windows October 2018 Update). You need to target a Windows 10 version if you want to use WinRT APIs.
In your project:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
</Project>
Also interesting:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.