1

Error:

The type or namespace name 'Forms' does not exist in the namespace 'System.Windows'

Code:

using System;
using System.Windows.Forms;

namespace SimpleCounter
{
    public partial class Form1 : Form
    {
        int counter = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            counter++;
            lblCounter.Text = counter.ToString();
        }

        private void btnSubtract_Click(object sender, EventArgs e)
        {
            counter--;
            lblCounter.Text = counter.ToString();
        }

        private void btnGolden_Click(object sender, EventArgs e)
        {
            counter += 2;
            lblCounter.Text = counter.ToString();
        }
    }
}

According to How do I add assembly references in Visual Studio Code? going to command palette and then typing NuGet: Add New Package and than typing using System.Windows.Forms should solve this but no options was found,

I am new to .NET and C# so this is very confusing for my first project.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • How did you create your project? Can you edit your question to show the contents of the project file (csproj)? – Jon Skeet Jan 19 '23 at 12:39
  • when you created the project, what did you do? if you use the "Windows Forms App" template in visual studio: everything will be setup correctly automatically; likewise if you use `dotnet new winforms` at the command line: the template will set everything up for you; otherwise, as @JonSkeet notes: you'd need to fix the csproj, which *we can't see* – Marc Gravell Jan 19 '23 at 12:39
  • I used `dotnet new` to create the file. –  Jan 19 '23 at 12:42
  • 1
    @CoolGuy `dotnet new` *what?* - `dotnet new console` ? `dotnet new wpf` ? `dotnet new classlib` ? – Marc Gravell Jan 19 '23 at 12:44
  • dotnet new console –  Jan 19 '23 at 12:46
  • 2
    @CoolGuy `dotnet new console` configures a terminal application, not a winforms application; the differences aren't huge, but honestly: if you haven't done much it is probably easier to create a new directory, do `dotnet new winforms` in there, and start from there (copying over anything you need). You *can* make the changes manually, but... do you really want to? – Marc Gravell Jan 19 '23 at 12:48
  • @Marc Gravell I used `dotnet new winforms` and now i got this new error like this: `The name 'lblCounter' does not exist in the current context` –  Jan 19 '23 at 12:49
  • Then that's an entirely different question - you should carefully try to fix it yourself (how do you *expect* it to be declared?) and then ask a new question if absolutely necessary. – Jon Skeet Jan 19 '23 at 12:49

1 Answers1

5

This comes down to the csproj, ultimately. If this is a .NET 7 project, then the following three entries are probably what you need (in a <PropertyGroup> in the project's csproj):

    <OutputType>WinExe</OutputType>
    <TargetFramework>net7.0-windows</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>

Note the some may already exist with different values - just replace them.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • That is exactly how it already was –  Jan 19 '23 at 12:46
  • @CoolGuy [that seems unlikely](https://i.stack.imgur.com/pannL.png) – Marc Gravell Jan 19 '23 at 12:50
  • @CoolGuy: That seems unlikely from `dotnet new console`. If you mean "after you'd already applied the change that Marc suggested" then you're actually talking about a different scenario with a different error. Marc is addressing the error you asked about in the question. – Jon Skeet Jan 19 '23 at 12:50
  • yes i have just realised that @Marc Gravell comment fix the problem and the error was for my code –  Jan 19 '23 at 12:53