-1

I've researched about this and I see people saying to open References then add Systen.Windows.Forms but for me it shows Dependencies and when I add System_Windows_Forms in it, it says there is a error with "Forms" Sample Code

using System.Windows.Form
namespace Session_Client 
{
    class Program
    {
        MessageBox.Show("Stackoverflow");
    }
}

It for some reason shows the options of Input and Markup both not including MessageBox.Show

Is there a possible solution?

DiplomacyNotWar
  • 31,605
  • 6
  • 57
  • 75
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Oct 24 '21 at 14:38
  • Console apps aren't supposed to have message boxes, they're supposed to write to the console. – user9938 Oct 24 '21 at 14:59
  • @user9938 Well yes, but actually no... Console apps can show message boxes and also can do graphical tasks/interfaces, they are not just for console/CLI style usage. – SimpleCoder Oct 24 '21 at 15:05
  • @SimpleCoder: I never said that it wasn't possible-it just doesn't make much sense to do so. – user9938 Oct 24 '21 at 15:34

1 Answers1

0

Your question is a little unclear, but here are two ways to show a message box in a c# console application.

Method 1:

First you need to include this reference.

System.Windows.Forms;

To add the reference:

You need to right click (in solution explorer) on your project name-> then add reference-> then .Net-> then select System.Windows.Forms. To add reference in c# program right click in your project folders shown in solution explorer on add references-> .Net -> select System.Windows.Forms.

then you can use a MessageBox in your application.

MessageBox.Show("Stackoverflow");

Method 2:

Alternatively you can create a simple MessageBox yourself using some code...

To do this you first need to create a property with the attribute of

using System.Runtime.InteropServices;
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr h, string m, string c, int type);

Then you can use this to call a MessageBox:

MessageBox((IntPtr)0, "Content", "Title", 0);

Answer formed from answers by Syed Osama Maruf and Nikhil Nambiar on this question related to the same problem.

SimpleCoder
  • 324
  • 3
  • 16
  • In the OP, it mentions "Dependencies" which indicates that it's a .NET console project - not .NET Framework, so you're "Method 1" won't work. – user9938 Oct 24 '21 at 15:43
  • @user9938 I have provided "Method 2" which will work, OP needs to specify his exact problem, It's not mentioned he is using .NET Core or .NET5, I figured to use the most common version. There are ways to import the DLL to .NET5 and .NET Core aswell. – SimpleCoder Oct 24 '21 at 18:53