準備
(なし)
デザイン
- フォーム (Form1) にボタン (button1) を配置します。
サンプルコード (C#)
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var button2 = new Button();
button2.Width = 145;
button2.Height = 62;
button2.Top = 21;
button2.Left = 200;
button2.Parent = this;
button2.Text = "Hello";
button2.Click += delegate (object sender2, EventArgs e2)
{
MessageBox.Show("Hello, World");
};
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
解説
ボタンをコードで作成しています。対応するイベントは匿名メソッドにより実現しています。
結果
動作確認環境
Visual Studio 2022 Professional (.NET 7 C#11)
ログ
初版:2016.05.03 Visual Studio 2015 Professional (C# 6.0)
