Cannot reference System.Windows.Forms.DataVisualization in a console app

MM 6 Reputation points
2022-12-02T20:42:33.403+00:00

Hi,

I'm in a Console App (.Net Framework) project and cant use windows.forms, I'm using a .Net Framework Console App because I was not able do it in a .Net Core app either. Specifically trying to call a class from System.Windows.Forms.DataVisualization. I understand that it is a windows form app but is there a work around? If not can some one explain why the common language runtime cannot doesn't allow me to reference something from the Forms module?

In order to find it at all in Console App I used nuget to retrieve HIC.System.Windows.Forms.DataVisualization, however when I try to reference it I get the below message. I'm new to .net/visual studio, is there a simple fix to this someone can assist me with?

"
A reference to 'System Windows_Forms' could not be added.

The ActiveX type library

'C:\Windows\Microsoft.NET\Framework64\v4.0.30319\System.Windo ws.Forms.tlb' was exported from a .NET assembly and cannot be added as a reference.

Add a reference to the .NET assembly instead.
"

Below is a screen shot of the error message:
266689-error-screenshot.png

I appreciate any help and/or info I can get.
Best regards.

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,835 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
323 questions
{count} vote

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,296 Reputation points Microsoft Vendor
    2022-12-05T08:21:43.267+00:00

    @MM , Welcome to Microsoft Q&A, based on my test, I could add the System.Windows.Forms.DataVisualization assembly in the console app successfully.

    First, please right-click Add-Reference and then choose the Assemblies tab.

    267113-image.png

    Second, please input System.Windows.Forms.DataVisualization in your searchbox and choose the correct option.
    267152-image.png

    Third, after clicking ok and you could use the assembly, like the following code:

    using System.Windows.Forms.DataVisualization.Charting;  
      
    namespace ConsoleApp1  
    {  
        internal class Program  
        {  
            static void Main(string[] args)  
            {  
                Chart chart = new Chart();  
      
            }  
        }  
    }  
    

    Hope my advice could help you.
    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.