//================================================================================================= // C#で作るイベントの基本(ボタン編) // Windowに表示されているボタンをクリックするとラベルの文字が変わるプログラム。 // ボタンを作るのは「Button」型で宣言し、後はラベルの感じで設定していく。 // // 参考文献:やさしいC#(p155) // 2012/11/12/オワタ //================================================================================================= using System; using System.Windows.Forms; class ButtonEvent : Form { private Label mLabel; //< ラベル private Button mButton; //< ボタン //< メイン関数 public static void Main() { Application.Run(new ButtonEvent()); } //< コンストラクタ public ButtonEvent() { //< Windowの設定 this.Text = "ボタンイベント"; this.Width = 250; this.Height = 100; //< ラベルの設定 mLabel = new Label(); mLabel.Text = "いらっしゃいませ!"; mLabel.Width = 150; //< ボタン生成 + 設定 mButton = new Button(); //< ボタン生成 mButton.Text = "購入"; //< ボタンの文字設定 mButton.Top = this.Top + mLabel.Height; //< ボタンの位置設定 mButton.Width = mLabel.Width; //< ボタンの横幅設定 //< Windowと同期 mLabel.Parent = this; mButton.Parent = this; //< ボタンをクリックでイベント処理 mButton.Click += new EventHandler(buttonClick); } //< ボタンを押した時に処理される関数 public void buttonClick(Object obj, EventArgs e) { mLabel.Text = "ありがとうございます!"; } }