如何在C#中从一个类中显示消息框?

30

如何让类与表单交互以显示一个消息框?


6
通常情况下,这不是一个好主意。理想情况下,类应该将数据发送回表单并由表单显示消息框。如果您在类中编写了UI特定的代码,则完全与该UI绑定。此外,这会使单元测试变得不可能。 - NotMe
4个回答

41
using System.Windows.Forms;
...
MessageBox.Show("Hello World!");

15
System.Windows.MessageBox.Show("Hello world"); //WPF
System.Windows.Forms.MessageBox.Show("Hello world"); //WinForms

4

试试这个:

System.Windows.Forms.MessageBox.Show("Here's a message!");

3
using System.Windows.Forms;

public class message
{
    static void Main()
    {  
        MessageBox.Show("Hello World!"); 
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接