Search code examples
c#messagebox

Shows red mark on MessageBox.Show


When I type - MessageBox.Show("Deleted!");

The C# editor red marks the 'MessageBox.Show' and shows

'The name MessageBox.Show doesn't exist in the current context. '

I have used 'using System.Windows.Forms;' Here also it red marks the 'Forms'.

Please Help Me. Thanks.


Solution

  • As you've deduced, MessageBox does indeed belong to System.Windows.Forms. Unless you have created a Windows Forms project, the reference to this library will not automatically be added to your project.

    You can add it to other project types by doing this:

    1. Right-click "References" under your project in the Solution Explorer
    2. Click "Add reference"
    3. Select "Assemblies"
    4. Check "System.Windows.Forms"
    5. Finally, click OK.

    After doing this, your using System.Windows.Forms should no longer show the red underline, and your message box code should work. Note that normally console output would be output to the console and not to a messagebox, hence why it isn't available by default.

    I don't believe message box functionality is available in .NET Core or .NET Standard (see here), so if you made a .NET Core Console Application (as opposed to .NET Framework), you might want to output information in a different way.