4

To set up my development framework for c# in visual studio code, I want to use the System.Windows.Forms refernce in order to set up a window.

Code:

using System;
using System.Collections;
using System.Windows.Forms;
namespace Softwaredevelopment
{
   public partial class Form1 : Form
   {
      public Form1()
      {
        //tbd
      }
   }
}

I get the error:

Missing Assembly Reference.

What do I have to add in the project.json file or the .csproj file to get the ability to create a window.

Rand Random
  • 7,300
  • 10
  • 40
  • 88
TimRicta
  • 135
  • 1
  • 2
  • 8
  • 5
    Is there a reason you want to do this? Windows Forms is windows only, so why not use the real Visual Studio? There is no point in using an inferior (but cross platform) editor if your product is never going to be cross-platform anyway. – nvoigt Nov 21 '17 at 12:12
  • 5
    @nvoigt: "Windows Forms is windows only" - that's not quite true, there is Windows Forms support for other OSes by means of [Mono](http://www.mono-project.com/docs/gui/winforms/). – O. R. Mapper Nov 21 '17 at 12:31
  • 2
    @Kyralessa Sometimes, an answer is not the best option. If someone asks "what is the best option to kill myself" the correct action is not to hand him a rope, but to get him help. Likewise, the OPs best course of action might be using a tool more suited to his needs. It might well be that he did not articulate his needs clearly enough for me to understand. That's why my comment contained question marks. – nvoigt Nov 21 '17 at 12:40
  • @Kyralessa nvoigt did offer the suggestion of using Visual Studio rather than VS Code. – Matthew Watson Nov 21 '17 at 12:42
  • 1
    @Kyralessa I'm not convinced you understand analogies. I guess we are both unconvinced. My advice stands as given: reevaluate whether VSCode is the right tool for the job. And no, this is not an answer by the SO standards. – nvoigt Nov 21 '17 at 12:48
  • 2
    @nvoigt: "reevaluate whether VSCode is the right tool for the job." - why should it *not* be the right tool for the job? There are various valid reasons why an installation of VS may not be desirable (e.g. the intention to work cross platform, lack of disk space, etc.). – O. R. Mapper Nov 21 '17 at 14:43
  • @O.R.Mapper The OP has problems with his toolset that would be automated away for him in the "default" toolset for his depicted task. So I think asking *why* he is not using the default toolset and instead making his life harder doing it manually is reasonable. – nvoigt Nov 21 '17 at 16:33
  • https://stackoverflow.com/questions/38460253/how-to-use-system-windows-forms-in-net-core-class-library This is for the old json format – kil98q Nov 21 '17 at 20:35
  • @Kyralessa There is a certain amount of irony in responding with a facetious comment after clearly deriding someone else for what you think is a pointless comment. – Adam Houldsworth Nov 22 '17 at 12:31
  • @Kyralessa I think he was asking questions that clarify the position because if those questions yielded a certain answer, the more correct answer is to not carry on down this avenue. There are lots of examples of this. You could argue this is another form of XY Problem. A comment, or at least some form of feedback is better than silence from the community, or better yet the dreaded impersonal "closed as duplicate" which is tantamount to LMGTFY. SO has issues with new user acceptance generally, but that comment isn't part of the problem. – Adam Houldsworth Nov 22 '17 at 13:37
  • @Kyralessa Being brutally honest, I don't give a monkeys about the whole topic - I just find it funny that it appears you trigger on a comment you feel is unhelpful then proceed to post an unhelpful comment yourself. – Adam Houldsworth Nov 22 '17 at 13:41

1 Answers1

5

Figured it out you can add this to your .csproj You can also find the names and versions here C:\Windows\assembly

  <Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <TargetFramework>net452</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="System.Drawing-dotnet-core" Version="1.2.3"/>

    <Reference Include="System.Windows.Forms" version="2.0.0.0" /> //< ----- add This line here

  </ItemGroup>
</Project>
kil98q
  • 51
  • 2
  • Great Simpler Answer, Sorry for newbie question, sometimes generated PackageReference and References have publickey attribute. Does any CLI command able to add Assemblies References like System.Windows.Forms, or nuget also support adding System.Windows.Forms. Some CLI command workflow for .NET is actually similar beginner friendly like PHP and Node.js, I wish there is more documentation everywhere, thus there is more microservices choice in .NET ecosystem – Edward Chan JW May 20 '18 at 08:07
  • I created a project with TimRicta's code and kil98q's project settings. build returns " Program does not contain a static 'Main' method suitable for an entry point ". What am I doing wrong? – user1164199 Mar 13 '19 at 20:31
  • @user1164199 what do you want to do? An executable: you need a static void main function. a library : you need to remove Exe – Pierre-olivier Gendraud Apr 16 '21 at 12:11