メッセージボックスを表示する
メッセージを表示するための新しいウインドウについて学ぶことにしましょう。
メッセージを表示する小さなウインドウは、メッセージボックス(MessageBox)と呼ばれています。まず、次のコードを入力してみて下さい。
using System;
using System.Windows.Forms;
using System.Drawing;
namespace lesson7
{
class sample11 : Form
{
private Label lb;
private Button bt;
static void Main(string[] args)
{
Application.Run(new sample11());
}
public sample11()
{
this.Text = "lesson7";
this.Width = 300; this.Height = 200;
lb= new Label();
lb.Text = "いらっしゃいませ";
lb.Dock = DockStyle.Top;
bt = new Button();
bt.Text = "購入";
bt.Dock = DockStyle.Bottom;
bt.Click += new EventHandler(bt_Click);
lb.Parent = this;
bt.Parent = this;
}
public void bt_Click(Object sender, EventArgs e)
{
MessageBox.Show("ご購入ありがとうございました。", "購入");
}
}
}
購入ボタンを押すと→
タイトルとメッセージを持った簡単なメッセージが表示されます。タイトルとメッセージを引数として指定しています。
かんたんなメッセージを表示するには、MessageBoxクラスを使う。
クラス | 説明 |
---|---|
System.Windows.Forms.MessageBoxクラス | |
DialogResult Show(string s, string t)メソッド | メッセージボックスにメッセージとタイトルを表示する |
メッセージボックスを変更する
using System;
using System.Windows.Forms;
using System.Drawing;
namespace lesson7
{
class sample12 : Form
{
private Label lb;
private Button bt;
static void Main(string[] args)
{
Application.Run(new sample12());
}
public sample12()
{
this.Text = "lesson7";
this.Width = 300; this.Height = 200;
lb= new Label();
lb.Text = "いらっしゃいませ";
lb.Dock = DockStyle.Top;
bt = new Button();
bt.Text = "購入";
bt.Dock = DockStyle.Bottom;
bt.Click += new EventHandler(bt_Click);
lb.Parent = this;
bt.Parent = this;
}
public void bt_Click(Object sender, EventArgs e)
{
DialogResult dr = MessageBox.Show("本当に購入しますか?", "確認",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
MessageBox.Show("ご購入ありがとうございました。", "購入", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
}
→ →
種類 | 表示 |
---|---|
OK | OK |
OKCancel | OK, キャンセル |
YesNo | はい、いいえ |
YesNoCancel | はい、いいえ、キャンセル |
種類 | 内容 | 表示 |
---|---|---|
Error | エラーアイコン | X |
Information | 情報アイコン | i |
Warning | 警告アイコン | ! |
Question | 質問アイコン | ? |
フォームをモーダルで表示する
メッセージボックスは簡単にメッセージを伝えたいときに便利です。しかし、より多くの情報を自由に伝えたい場合もあります。
そこで、今度はボタンを押したときに新しいフォームを作成し、表示してみることにしましょう。
using System;
using System.Windows.Forms;
using System.Drawing;
namespace lesson7
{
class sample13 : Form
{
private Label lb;
private Button bt;
static void Main(string[] args)
{
Application.Run(new sample13());
}
public sample13()
{
this.Text = "lesson7";
this.Width = 300; this.Height = 200;
lb= new Label();
lb.Text = "いらっしゃいませ";
lb.Dock = DockStyle.Top;
bt = new Button();
bt.Text = "購入";
bt.Dock = DockStyle.Bottom;
bt.Click += new EventHandler(bt_Click);
lb.Parent = this;
bt.Parent = this;
}
public void bt_Click(Object sender, EventArgs e)
{
SampleForm sf = new SampleForm();//新しいフォームを作成します
sf.ShowDialog(); //新しいフォームをモーダルを表示します
}
}
class SampleForm : Form //新しいフォームを定義します
{
public SampleForm()
{
this.Text = "御礼";
this.Width = 300; this.Height = 200;
Label lb = new Label();
lb.Text = "ありがとうございました。";
lb.Dock = DockStyle.Top;
Button bt = new Button();
bt.Text = "OK";
bt.Dock = DockStyle.Bottom;
lb.Parent = this;
bt.Parent = this;
}
}
}
購入ボタンを押す→
今度は新しいフォームを定義しておきます。ここではSampleFormという新しいフォームを定義しています。このフォームは、これまで作成しているフォームと同様に、Formクラスを拡張したクラスです。
このSampleFormを作成してから、FormクラスのShowDialog()メソッドを呼び出すことによってフォームを表示します。
ただし、この新しいフォームは、ユーザーが「OK」ボタンを押すかフォームの「閉じる」ボタンを押すまでは、ほかのウィンドウの操作ができない状態となります。この状態はモーダル(modal)と呼ばれています。
ShowDialog()メソッドでモーダルなフォームを表示できる
クラス | 説明 |
---|---|
System.Windows.Forms.Formクラス | |
DialogResult ShowDialog()メソッド | フォームをモーダルで表示する |
System.Windows.Forms.Buttonクラス | |
DialogResultプロパティ | 親フォームに返す値を設定・取得する |
フォームをモードレスで表示する
「モーダル」な状態では、新しいフォームが表示されてる状態では元のフォームを操作することができないようになってます。
これに対して、新しいフォームが表示されても、元のフォームや他のフォームの入力もできるようにすることができます。この状態を、モードレス(modeless)といいます。
モードレスでフォームを表示するには、表示する際にShow()メソッドを使います。
using System;
using System.Windows.Forms;
using System.Drawing;
namespace lesson7
{
class sample14 : Form
{
private Label lb;
private Button bt;
static void Main(string[] args)
{
Application.Run(new sample14());
}
public sample14()
{
this.Text = "lesson7";
this.Width = 300; this.Height = 200;
lb= new Label();
lb.Text = "いらっしゃいませ";
lb.Dock = DockStyle.Top;
bt = new Button();
bt.Text = "購入";
bt.Dock = DockStyle.Bottom;
bt.Click += new EventHandler(bt_Click);
lb.Parent = this;
bt.Parent = this;
}
public void bt_Click(Object sender, EventArgs e)
{
SampleForm sf = new SampleForm();//新しいフォームを作成します
sf.Show(); //新しいフォームをモードレスを表示します
}
}
class SampleForm : Form //新しいフォームを定義します
{
public SampleForm()
{
this.Text = "新規";
this.Width = 300; this.Height = 200;
Label lb = new Label();
lb.Text = "新しい店をはじめました。";
lb.Dock = DockStyle.Top;
Button bt = new Button();
bt.Text = "OK";
bt.Dock = DockStyle.Bottom;
lb.Parent = this;
bt.Parent = this;
bt.Click += new EventHandler(bt_Click);
}
public void bt_Click(Object sender, EventArgs e)
{
this.Close();
}
}
}
購入ボタンを押す→
今度は新しいフォームが表示されても、元のフォームを操作することができます。新しいフォームを2つ以上表示することも可能になります。
Show()メソッドでモードレスなフォームを表示できる。
クラス | 説明 |
---|---|
System.Windows.Forms.Controlクラス | |
void show()メソッド | コントロールをモードレスで表示する |
コメント