如何在 .net c# 或 vb 控制台应用程序中显示消息框? 比如:
Console.WriteLine("Hello World");
MessageBox.Show("Hello World");
或
Console.WriteLine("Hello")
MsgBox("Hello")
分别在 C# 和 VB 中。
可以吗?
我们可以在控制台应用程序中显示消息框。但首先请将此引用包含在您的 vb.net 或 c# 控制台应用程序中
System.Windows.Forms;
参考:
要在 vb.net 程序中添加引用,请右键单击(在解决方案资源管理器中)项目名称 -> 然后添加引用 -> 然后 .Net -> 然后选择 System.Windows.Forms。
要在 C# 程序中添加引用,请右键单击解决方案资源管理器中显示的项目文件夹,然后添加引用 -> .Net -> 选择 System.Windows.Forms。
然后您可以为 C# 控制台应用程序执行以下代码:
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对相关问题的回答。
要在控制台应用程序中拥有一个简单的消息框,您可以按照以下步骤操作。
创建属性为
的属性 using System.Runtime.InteropServices;
[DllImport("User32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr h, string m, string c, int type);
使用该属性来调用消息框。
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);
}
}
}
在C#中,在项目中添加引用“PresentationFramework”。接下来在课程中您需要
MessageBox
添加
using System.Windows;
您也可以调用
MessageBox
类,而无需像这样使用:
System.Windows.MessageBox.Show("Stackoverflow");
对于 .NET 5 和 .NET 6 =>
按照常规方式创建控制台应用程序。
使用以下之一更新 .csproj 中的 TargetFramework:
将其添加到.csproj:
编译应用程序,以便更新引用的.NET dll。
对于 WPF 消息框,在代码文件顶部添加 using System.Windows;,对于 Windows 窗体消息框,添加 using System.Windows.Forms;。然后,只需调用 MessageBox.Show("...")