Show message Box in .net console application
Asked Answered
D

4

43

How to show a message box in a .net c# or vb console application ? Something like:

 Console.WriteLine("Hello World");
 MessageBox.Show("Hello World");

or

Console.WriteLine("Hello")
MsgBox("Hello")

in c# and vb respectively.
Is it possible?

Decagram answered 29/3, 2015 at 5:45 Comment(0)
D
52

We can show a message box in a console application. But first include this reference in your vb.net or c# console application

System.Windows.Forms;

Reference:

To add reference in vb.net program 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 do the below code for c# console application:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {


            MessageBox.Show("Hello World");
        }
    }
}

For the vb.net application you can simply code after inclusion of above mentioned reference

Module Module1

    Sub Main()
        MsgBox("Hello")
        Console.ReadKey()


    End Sub

End Module

Adapted from this answer to a related question.

Decagram answered 29/3, 2015 at 5:45 Comment(1)
Move Add reference explanation top because code is depended on referenceRybinsk
W
36

To have a simple message box inside your console application you can follow the below steps.

  1. Create a property with 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);
    
  2. User the property to call the message box.

     using System;
     using System.Runtime.InteropServices;
    
     namespace AllKeys
     {
         public class Program
         {
             [DllImport("User32.dll", CharSet = CharSet.Unicode)]
             public static extern int MessageBox(IntPtr h, string m, string c, int type);
    
             public static void Main(string[] args)
             {
                 MessageBox((IntPtr)0, "Your Message", "My Message Box", 0);
             }
         }
     }
    
Wrung answered 16/11, 2016 at 11:31 Comment(1)
I think this answer must be accepted answer, because we need't add windows form application's dependencies to console app (For example using System.Windows.Forms;)Mallorymallow
B
5

For .NET 5 and .NET 6 =>

  1. Create the console app the regular way.

  2. Update the TargetFramework in .csproj with one of these:

    <TargetFramework>net5.0-windows</TargetFramework>

    <!--OR-->

    <TargetFramework>net6.0-windows</TargetFramework>

  3. Add this to .csproj:

    <UseWPF>true</UseWPF>

    <!-- AND/OR -->

    <UseWindowsForms>true</UseWindowsForms>

  4. Compile the app, so the referenced .NET dlls get updated.

  5. For the WPF message box add using System.Windows; and for the Windows Forms message box add using System.Windows.Forms; on the top of your code file. Then, just make calls to MessageBox.Show("...")

Brittnybritton answered 17/1, 2022 at 18:35 Comment(2)
Reference: learn.microsoft.com/en-US/dotnet/core/project-sdk/…Brittnybritton
Alos, if you want to hide the console window, here is the solution: #3854129Brittnybritton
A
4

In C# add the reference "PresentationFramework" in the project. Next in the class that you need the MessageBox add

using System.Windows;

also you can call the MessageBox class without the using like that:

System.Windows.MessageBox.Show("Stackoverflow");
Anonym answered 20/5, 2019 at 14:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.