在.net控制台应用程序中显示消息框

时间:2015-03-29 05:45:14

标签: c# .net vb.net console-application messagebox

如何在.net c#或vb 控制台应用程序中显示消息框? 类似的东西:

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

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

分别在c#和vb中  有可能吗?

3 个答案:

答案 0 :(得分:28)

我们可以在控制台应用程序中显示一个消息框。但首先在您的vb.net或c#console应用程序中包含此引用

System.Windows.Forms;

参考:

要在vb.net程序中添加引用,请右键单击(在解决方案资源管理器中)项目名称 - >然后添加参考 - >然后.Net->然后选择System.Windows.Forms 要在c#程序中添加引用,请右键单击解决方案资源管理器中显示的项目文件夹,然后添加引用 - > .Net - >选择System.Windows.Forms。

然后你可以为c#console应用程序执行以下代码:

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

对于vb.net应用程序,您可以在包含上述参考

后进行简单编码
Module Module1

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


    End Sub

End Module

改编自this回答相关问题。

答案 1 :(得分:11)

要在控制台应用程序中放置一个简单的消息框,您可以按照以下步骤操作。

  1. 创建属性为
  2. 的属性
      

    使用System.Runtime.InteropServices;

    [DllImport("User32.dll", CharSet = CharSet.Unicode)]
    
    public static extern int MessageBox(IntPtr h, string m, string c, int type);
    
    1. 使用该属性调用消息框。

      MessageBox((IntPtr)0," asdasds"," My Message Box",0);

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

答案 2 :(得分:0)

在C#中,在项目中添加引用“ PresentationFramework”。在课程的下一个步骤,您需要MessageBox添加

using System.Windows;

您也可以调用MessageBox类,而无需这样使用:

System.Windows.MessageBox.Show("Stackoverflow");
相关问题