Csharp/CSharp Tutorial/GUI Windows Forms/MessageBox

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Call Show method to display a MessageBox

class MessageBoxHelloWorld {
    public static void Main() {
        System.Windows.Forms.MessageBox.Show("Hello, world!");
    }
}

Display MessageBox in Button event

using System; 
using System.Windows.Forms; 
using System.Drawing; 
 
class ButtonEventMessageBox : Form { 
  Button StopButton; 
 
  public ButtonEventMessageBox() { 
    Text = "Adding a Stop Button"; 
 
    StopButton = new Button(); 
    StopButton.Text = "Stop"; 
    StopButton.Location = new Point(100, 100); 
 
    StopButton.Click += StopButtonClick; 
    Controls.Add(StopButton); 
  }   
 
  [STAThread] 
  public static void Main() { 
    ButtonEventMessageBox skel = new ButtonEventMessageBox(); 
 
    Application.Run(skel); 
  } 
 
  protected void StopButtonClick(object who, EventArgs e) { 
    DialogResult result = MessageBox.Show("Stop Program?", 
                            "Terminate", 
                            MessageBoxButtons.YesNo); 
 
    if(result == DialogResult.Yes) 
        Application.Exit(); 
  } 
}

Get result from a MessageBox

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class MainClass
{
    public static void Main(){
        DialogResult result = MessageBox.Show("Close?", "Really quit?",
            MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        if (result == DialogResult.No)
            Console.WriteLine("NO");
        else{
            Console.WriteLine("Yes");
        }    
    }
}
Yes

Show method with button and icon settings

using System;
using System.Windows.Forms;
public class MainClass {
    public static void Main() {
        DialogResult dr = MessageBox.Show("Do you want to create a new file?",
                                          "WonderWord",
                                          MessageBoxButtons.YesNoCancel,
                                          MessageBoxIcon.Question);
        if (dr == DialogResult.Yes) {
            // "Yes" processing
        } else if (dr == DialogResult.No) {
            // "No" processing
        } else {
            // "Cancel" processing
        }
    }
}

Show method with message and window title

using System;
using System.Windows.Forms;
class MainForm : Form {
    [STAThread]
    public static void Main() {
        MessageBox.Show("Welcome," , "Visual C#");
    }
}

Simplest MessageBox

using System;        
using System.Windows.Forms;
class MainClass
{
     public static void Main()
     {
        MessageBox.Show("Hello...");
     }
}