简体   繁体   中英

Send MessageBox Via C# console application

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?

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

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");

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM