I have tried making (my first) a C# program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
Console.ReadLine();
}
}
}
This goes well, but if I try using System.Windows.Forms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("hello");
System.MessageBox("hello");
Console.ReadLine();
}
}
}
This is the error I get:
Error 1 The type or namespace name 'Windows' does not exist in the namespace 'System' (are you missing an assembly reference?) C:\Users\Ramy\Documents\Visual Studio 2010\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs 5 14 ConsoleApplication1
Some details: - I am using Visual Studio 2012; - I have installed the .NET Development Kit; - It is a Console Application.
Maybe it's because on a Console Application can't use System.Windows.Forms? If so, what program should be? I also have tried with a form, but I was only displaying a window and no code.
A console application does not automatically add a reference to System.Windows.Forms.dll.
Right-click your project in Solution Explorer and select Add reference... and then find System.Windows.Forms and add it.
Sorry to revive this thread, but I created an account just to do so, as none of the solutions I found searching google for days worked for me alone, and seemed to only bring up only outdated tutorials.
Not Working =(
Add the following lines (replacing the existing TargetFramework line):
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
My window, for referrence, looks like:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
I stumbled on this solution after applying the "UseWindowsForms" tag, and then in desperation changing my NET framework in properties to 5.0, which caused it to work, and then noted when changing back to 6.0 it still worked.
If you forget the Framework tag in the future, you can reproduce the effect just by flipping your properties back and forth...
Right click the project, and go to properties.
Change the Target framework from .NET 6.0...to....NET 5.0
Exit back to your code. Which after a few moments will show the System.Windows.Forms connecting properly.
Open back up properties.
Change the Target framework from .Net 5.0, back to .Net 6.0
You have (or at least I have) a .Net 6.0 project that is properly allowing me to use System.Windows.Forms (Including the Clipboard, which I suspect many here are looking for...).
I did due diligence on this, testing it multiple times across multiple projects, and it (at least for my setup) consistently works!
BTW. For those wondering. the [STAThread] Attribute tag seen in the pictures is needed to allow the Clipboard class to function. (this is also why I am not using top level statements in the example, but if you don't need that Class, the example works with top level statements (I needed it to show my Clipboard test....)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With