30

How do you get a class to interact with the form to show a message box?

Litisqe Kumar
  • 2,512
  • 4
  • 26
  • 40
Ash
  • 8,583
  • 10
  • 39
  • 52
  • 6
    This isn't normally a good idea. Ideally, the class should send the data back to the form and let the form show the message box. If you code UI specifics into your class then it is completely tied to that UI. Also, it makes unit testing impossible. – NotMe Apr 03 '09 at 19:08

4 Answers4

41
using System.Windows.Forms;
...
MessageBox.Show("Hello World!");
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
15
System.Windows.MessageBox.Show("Hello world"); //WPF
System.Windows.Forms.MessageBox.Show("Hello world"); //WinForms
Mehrdad Afshari
  • 414,610
  • 91
  • 852
  • 789
4

Try this:

System.Windows.Forms.MessageBox.Show("Here's a message!");
Keith Gaughan
  • 21,367
  • 3
  • 32
  • 30
3
using System.Windows.Forms;

public class message
{
    static void Main()
    {  
        MessageBox.Show("Hello World!"); 
    }
}
Konstantin Tarkus
  • 37,618
  • 14
  • 135
  • 121