87

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?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
John Smith
  • 1,351
  • 3
  • 16
  • 21
  • 18
    Add a reference to `System.Windows.Forms` – ta.speot.is Jul 10 '11 at 05:48
  • 3
    Sounds like you created a WPF project rather than a Windows Forms project – shf301 Jul 10 '11 at 05:51
  • 3
    @todda, thanks, that worked :) @shf301, Yes it was a WPF project, but adding the mentioned reference worked. – John Smith Jul 10 '11 at 06:10
  • 2
    For future reference, when asking an SO question, please paste the *exact* code, which is very much case-sensitive among other things... –  Jul 10 '11 at 06:52
  • 2
    @sara Regarding your bounty, what kind of answer are you looking for here? What "official sources" do you need? The question has clearly been answered: you cannot use items from namespaces that you have not added references to. Are you looking for a citation from the language standard that makes the same point? – Cody Gray - on strike Feb 29 '16 at 11:03
  • i am facing this issue in console application. how can i resolve this issue. ? – chandu komati May 25 '20 at 15:18

8 Answers8

145

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.

For people looking for VS Code: How do I add assembly references in Visual Studio Code

VMAtm
  • 27,943
  • 17
  • 79
  • 125
77

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>
Fudge
  • 49
  • 4
jool
  • 1,491
  • 12
  • 14
  • 7
    This is exactly the solution that worked for me. Many of the proposed solutions I was seeing suggested adding a reference to "System.Window.Forms", but that proposal never worked for me. I was able to add that reference and resolve my missing Forms class, but by adding that reference, it broke "System.Window" – Tom Rutchik Mar 25 '20 at 21:22
  • Strange, it works on one computer, but open on another and it failed. The solution was UseWindowsForms tag. You would think with the problems of Windows Forms in .net core, Visual Studio would offer something more useful in both error finding and letting you add forms in the gui without having to close, manually edit and reload... – Neil Jan 29 '21 at 16:38
  • Thanks. Also, It was necessary for me to change targetframework from net5.0 to net5.0-windows. – mfvjunior Mar 10 '21 at 12:52
  • @mfvjunior, yes, full explained here: https://stackoverflow.com/a/66098428/842935 – dani herrera Aug 12 '22 at 07:57
  • Used this answer to use old MS C# example code (aimed at 4.0) with Net 7.0. Only other thing I needed to do was to resolve an ambiguity with `Application`, by fully qualifying it with `System.Windows.Forms.Application`. https://learn.microsoft.com/en-us/previous-versions/dotnet/articles/aa480727(v=msdn.10)?redirectedfrom=MSDN – DS_London Mar 06 '23 at 17:43
20

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.

Owlbuster
  • 173
  • 1
  • 5
Bizhan
  • 16,157
  • 9
  • 63
  • 101
14

Net >= 5

<TargetFramework>
   net5.0-windows
</TargetFramework>

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:

  • net5.0 is the new Target Framework Moniker (TFM) for .NET 5.0.
  • net5.0 combines and replaces netcoreapp and netstandard TFMs.
  • net5.0 supports .NET Framework compatibility mode
  • net5.0-windows will be used to expose Windows-specific functionality, including Windows Forms, WPF and WinRT APIs.
  • .NET 6.0 will use the same approach, with net6.0, and will add net6.0-ios and net6.0-android.
  • The OS-specific TFMs can include OS version numbers, like net6.0-ios14.
  • Portable APIs, like ASP.NET Core will be usable with net5.0. The same will be true of Xamarin forms with net6.0.
dani herrera
  • 48,760
  • 8
  • 117
  • 177
2

You may encounter this problem if you have multiple projects inside a solution and one of them is physically located inside solution folder. I solved this by right click on this folder inside Solution tree -> then pressing "exclude from project"

exclude folder

Skeptik
  • 49
  • 1
  • 4
0

For some one just need the struct and some function from Windowsform namespace, you just need to change the the project to old behave.

Make DisableWinExeOutputInference true then OutputType dont be override by visual studio :D.

Dont foget set add -windows to TargetFramework

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0-windows</TargetFramework>
<DisableWinExeOutputInference>true</DisableWinExeOutputInference>
<UseWindowsForms>true</UseWindowsForms>
<StartupObject></StartupObject>
<ApplicationIcon />
</PropertyGroup>

here where i found them https://learn.microsoft.com/en-us/dotnet/core/compatibility/sdk/5.0/sdk-and-target-framework-change

0

The cleanest solution is to add this nuget package Link to nuget pacakge page

Vitch612
  • 31
  • 1
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34047901) – Jean Jimenez Mar 23 '23 at 12:45
-1
browxy.com 
Compilation failed: 1 error(s), 0 warnings
main.cs(7,24): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?

browxy.com

CS QGB
  • 297
  • 1
  • 3
  • 12