locked
Error Message "the name MessageBox does not exist ..."

    Question

  • Hello,

    I am trying to learn more about c-sharp. I created the console program shown below which reads a text file one line at a time.

    The program works well. However, when I add a MessageBox.Show(@"message"); statement, I received the following error message:

    "the name MessageBox does not exist in the current context"

    I see others have had this same issue, and so far, none of the solutions I've seen resolve the issue in this situation.

    I tried adding a statement "using System.Windows.Forms;" but that created an error on the word "Windows".

     

    What is the problem here, and what can I do to resolve it?

     

     

     

     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    // This is a c sharp console program
    namespace Read_a_File_in_C_Sharp_1
    {
    class Program
    {
    static void Main(string[] args)
    {
    string filePath = @"c:\temp\testFile1.txt";
    
    string line;
    MessageBox.Show(@"Test Point in Main"); // the name MessageBox does not exist in the current context
    if (File.Exists(filePath))
    {
    StreamReader file = null;
    try
    {
    file = new StreamReader(filePath);
    while ((line = file.ReadLine()) != null)
    {
    Console.WriteLine(line);
    }
    }
    finally
    {
    if (file != null)
    file.Close();
    }
    }
    Console.ReadLine();
    }
    }
    }

     

     

     

    Tuesday, May 03, 2011 12:33 PM

Answers

  • Hi,

    Using this statement and referencing a DLL are not the same thing. Use Project, References and select the System.Windows.Forms to add the reference. It allows to tell which DLL you would like to be able to use.

    The using statement is a shortcut that tells basically that if a name is not known it has to be found there (i.e. your code would work with System.Windows.Forms.MessageBox wihtout the using statement, if you want to use only MessageBox then the "using" statement is needed).

    As a side note, try to take the habit to always tell explicitely which error you received rather than telling that you "receive an error". In more advanced scenario it could waste the time of those who are willing to help.

    Finally I would strongly suggest to study a good reference tutorial (ask for in another thread if needed). IMO a forum is not very appropriate to learn the basics (of course, feel free to ask, but try to study a tutorial first).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Marked as answer by Keith17 Tuesday, May 03, 2011 3:56 PM
    Tuesday, May 03, 2011 2:04 PM

All replies

  • You haven't not added System.Windows.Forms dll as reference assembly and since its a console application, you need to decorate the main function to the following

     [STAThread]
    
    attribute


    Pratap --Mark the best replies as answers!
    Tuesday, May 03, 2011 12:43 PM
  • Thank you very much.

    I'm not understanding something here.

    I tried adding "using System.Windows.Forms" among the using statements and received an error on the word "Windows".

    I'm new to c sharp, so [STAThread] is a new term to me.  That goes in main?  Is there an example somewhere?

     

    Keith

    Tuesday, May 03, 2011 1:20 PM
  • Hi,

    Using this statement and referencing a DLL are not the same thing. Use Project, References and select the System.Windows.Forms to add the reference. It allows to tell which DLL you would like to be able to use.

    The using statement is a shortcut that tells basically that if a name is not known it has to be found there (i.e. your code would work with System.Windows.Forms.MessageBox wihtout the using statement, if you want to use only MessageBox then the "using" statement is needed).

    As a side note, try to take the habit to always tell explicitely which error you received rather than telling that you "receive an error". In more advanced scenario it could waste the time of those who are willing to help.

    Finally I would strongly suggest to study a good reference tutorial (ask for in another thread if needed). IMO a forum is not very appropriate to learn the basics (of course, feel free to ask, but try to study a tutorial first).


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    • Marked as answer by Keith17 Tuesday, May 03, 2011 3:56 PM
    Tuesday, May 03, 2011 2:04 PM
  • Thank you very much. Your comments led me to solution.

    As a side note, my first post, above, stated the exact error message as it appeared...

    "the name MessageBox does not exist in the current context"

    In my searches of documentation, forums, and book sources, this seems to be a common error, but the proposed solutions are not all the same.  Perhaps this error message can occur for a number of different reasons.  

    Again, thank you very much to both responders, Pratap and Patrice.

    Keith

     

    Tuesday, May 03, 2011 3:56 PM

  • Do not worry about STAThread if you are only using MessageBox.Show() in a console app.  It is not necessary to what you are trying to do.

    --
    Mike
    Tuesday, May 03, 2011 11:01 PM
  • Keith,

    The scenarios differ greatly and MessageBox is one among the many things which have many scenarios, where many things might be wrong.

    Say, for example. I am using a ASP.Net web project. In a web project, you cannot have a MessageBox. You maybe able to fix the error by adding a reference by using the using keyword, but that wouldnt show a messagebox on the client application. The right way to do it there is by using javascript in general (usually by using a window.alert(Message) command).

    In another scenario, where I come across is the windows forms itself. Whenever I complete writing a class, I go for the Remove Unused usings option from Visual Studio IDE. Then at a later point of time, when I try to add the MessageBox component, it gives me an error because my reference was deleted by me previously.

    There may be other cases, but top of my head, those 2 are the ones that I come across very very often.

    The 3rd would be like Patrice suggested. If your assembly is missing references, then even if you do use the using keyword and write a namespace, it is not going to work, as Patrice explained, using is just a shorthand notation to work with the dll being referenced.

    Wednesday, May 04, 2011 12:56 AM
  • Oups, sorry and well done. I missed the error message in your first post (though the first error message you had and the one you had when adding using System.Windows etc... were likely not the same). Great it's solved anyway...
    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Wednesday, May 04, 2011 8:07 AM
  • Yes, Thank you all very very much.

    This is helpful information.

    Keith

     

    Wednesday, May 04, 2011 1:33 PM