Imports System.Windows.Forms Imports Microsoft.Win32 Imports System.Drawing Imports System.Drawing.Imaging Public Class Form1 Private testButtons() As System.Windows.Forms.Button Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'ボタンコントロール配列の作成(ここでは5つ作成) Me.testButtons = New System.Windows.Forms.Button(4) {} 'ボタンコントロールのインスタンス作成し、プロパティを設定する Me.SuspendLayout() Dim i As Integer For i = 0 To Me.testButtons.Length - 1 'インスタンス作成 Me.testButtons(i) = New System.Windows.Forms.Button 'プロパティ設定 Me.testButtons(i).Name = "Button" + i.ToString() Me.testButtons(i).Text = i.ToString() Me.testButtons(i).Size = New Size(30, 30) Me.testButtons(i).Location = New Point(i * 30, 10) 'イベントハンドラに関連付け AddHandler Me.testButtons(i).Click, AddressOf Me.testButtons_Click AddHandler Me.testButtons(i).MouseDown, AddressOf Me.testButtons_MouseDown AddHandler Me.testButtons(i).MouseUp, AddressOf Me.testButtons_MouseUp Next i 'フォームにコントロールを追加 Me.Controls.AddRange(Me.testButtons) Me.ResumeLayout(False) End Sub Private Sub testButtons_Click(ByVal sender As Object, ByVal e As EventArgs) 'クリックされたボタンのNameを表示する MessageBox.Show(CType(sender, System.Windows.Forms.Button).Name) End Sub Private Sub testButtons_MouseDown(sender As Object, e As MouseEventArgs) DirectCast(sender, Button).BackColor = Color.Red End Sub Private Sub testButtons_MouseUp(sender As Object, e As MouseEventArgs) DirectCast(sender, Button).BackColor = Color.Blue End Sub End Class