主題の要件:
左のようにフォームをデザインします。フォームには2つのボタンがあります。1つはテキストを表示するためのもので、もう1つは画像を表示するためのものです。
(1)上のボタンをクリックするか、Alt + Bを押すと、右側に表示されるメッセージボックスがポップアップ表示されます。
(2)下のボタンをクリックすると、右側に表示されているメッセージボックスもポップアップ表示されます(メッセージダイアログボックスについては、教科書のセクション6.5.1を参照してください)。
参照コード:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication5_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//this.button1.Text = "第一个按钮(&B)";
this.button2.BackgroundImage = Image.FromFile("picture1.jpg");
this.button2.BackgroundImageLayout = ImageLayout.Stretch;
this.button2.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.B && e.Alt)
{
MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
}
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("单击了我", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
}
}
}
自動生成されたDesignerファイル:
namespace WindowsFormsApplication5_2
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(73, 39);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(360, 87);
this.button1.TabIndex = 0;
this.button1.Text = "第一个按钮(&B)";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(73, 152);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(360, 91);
this.button2.TabIndex = 1;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(522, 314);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.KeyPreview = true;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}