Windowを閉じる際、メッセージボックスを表示して終了の確認を行うサンプルです。
-------------------------------------------------------
using System;
using System.Windows.Forms;
class HelloWorld : Form
{
private Button mybutton;
public static void Main()
{
Application.Run(new HelloWorld());
}
public HelloWorld()
{
this.mybutton = new Button();
this.SuspendLayout();
this.Text = "Hello World!";
this.mybutton.Location = new System.Drawing.Point(200, 200);
this.mybutton.Text = "閉じるよ";
this.mybutton.Click += new EventHandler(this.mb_Click);
this.Controls.Add(mybutton);
this.ResumeLayout(false);
}
private void mb_Click(object sender, EventArgs e)
{
this.Close();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
base.OnClosing(e);
DialogResult dr = MessageBox.Show("終了していいですか",
"終了確認",
MessageBoxButtons.YesNo);
if(dr==DialogResult.No) e.Cancel = true;
}
}
-------------------------------------------------------
Windowを閉じる処理の中断の仕方、MessageBoxの使い方、イベントのオーバーライドのサンプルになってます。
あおい情報システム株式会社 小野修司(どっとねっとふぁん)