Problems with MessageBox.Show()

17,226

Solution 1

There is no such namespace System.Forms, the class you were trying to use (MessageBox) is in System.Windows.Forms. By correcting your using statement, the error went away.

Remember, you must have a reference to System.Windows.Forms.dll in your console app to use this class.

Solution 2

You need to reference System.Windows.Forms.dll in your project. Here is a detailed instruction how to do that.

Solution 3

There is no such namespace as System.Forms there is only a namespace called System.Windows.Forms, wich has the MessageBox class you are talking about. To be able to use it, you need to add a reference to the System.Windows.Forms.dll to to your project (find it in the .NET Tab in the "Add Reference ..." dialog) and it will work. Also note that MessageBox.Show() requires a capital 'S'. Please see below an optimized and fully working version of your code.

using System.IO;
using System.Windows.Forms;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader sr = new StreamReader(@"file.csv"))
            {
                while (sr.Peek() >= 0)
                {
                    string strline = sr.ReadLine();
                    string[] values = strline.Split(',');
                    if (values.Length >= 6 && values[0].Trim().Length > 0)
                    {
                        MessageBox.Show(values[1]);
                    }
                }
            }
        }
    }
}

Solution 4

You try use it in Console application first you should add System.Windows.Forms dll in your references (from .Net reference tab) then use it by adding it's namespace.

Solution 5

I'm a bit confused here. there is no namespace called System.Forms. It's always System.Windows.Forms. And the MessageBox class is defined in System.Windows.Forms

You need to manually ADD a reference to your project for System.Windows.Forms as you are on a console application and not a Windows Application. Just add the reference.

Share:
17,226
Tim
Author by

Tim

Updated on July 23, 2022

Comments

  • Tim
    Tim almost 2 years

    I'm new to code and most things work, but I can't get this code to run. Can someone help?

    I tried using System.Forms but it showed as missing a namespace. When I used using System.Windows.Forms that message went away. It does not let me use both.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Windows.Forms;
    
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                StreamReader sr = new StreamReader(@"file.csv");
                // for set encoding
                // StreamReader sr = new StreamReader(@"file.csv", Encoding.GetEncoding(1250));
    
                string strline = "";
                String[] _values = null;
                int x = 0;
                while(!sr.EndOfStream)
                {
                    strline = sr.ReadLine();
                    _values = strline.Split(',');
                    if (_values.Length >= 6 && _values[0].Trim().Length > 0)
                    {
                        MessageBox.show(_values[1]);
                    }
                }
                sr.Close();
    
            }
        }
    }