効率のために、そのような大量のコントロールを作成したくはありません。独自の単一の描画面にすべての座席を描画する単一のカスタム コントロールを作成することをお勧めします。非常に簡単な例を次に示します。
Public Class SeatingPlan
Public Class Seat
Public Rectangle As Rectangle
Public Selected As Boolean
Public Id As String
Public Sub New(ByVal seatId As String, ByVal x As Integer, ByVal y As Integer, ByVal width As Integer, ByVal height As Integer)
Id = seatId
Rectangle = New Rectangle(x, y, width, height)
End Sub
End Class
Public ReadOnly Property Seats() As List(Of Seat)
Get
Return _seats
End Get
End Property
Private _seats As List(Of Seat) = New List(Of Seat)()
Public Event SelectedSeatsChanged()
Private Sub SeatingPlan_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
For Each seat As Seat In _seats
If seat.Rectangle.Contains(e.Location) Then
seat.Selected = Not seat.Selected
RaiseEvent SelectedSeatsChanged()
Exit For
End If
Next
Invalidate()
End Sub
Private Sub SeatingPlan_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
For Each seat As Seat In _seats
Dim seatBackColor As Color = BackColor
Dim textColor As Color = ForeColor
If seat.Selected Then
seatBackColor = Color.FromKnownColor(KnownColor.Highlight)
textColor = Color.FromKnownColor(KnownColor.HighlightText)
End If
e.Graphics.FillRectangle(New SolidBrush(seatBackColor), seat.Rectangle)
e.Graphics.DrawRectangle(New Pen(ForeColor), seat.Rectangle)
Dim textSize As SizeF = e.Graphics.MeasureString(seat.Id, Me.Font, seat.Rectangle.Width)
e.Graphics.DrawString(seat.Id, Font, New SolidBrush(textColor), seat.Rectangle.X + ((seat.Rectangle.Width - textSize.Width) / 2), seat.Rectangle.Y + ((seat.Rectangle.Height - textSize.Height) / 2))
Next
End Sub
Public Function GetSelectedSeatIds() As List(Of String)
Dim ids As List(Of String) = New List(Of String)()
For Each seat As Seat In _seats
If seat.Selected Then
ids.Add(seat.Id)
End If
Next
Return ids
End Function
Public Sub SetSelectedSeatIds(ids As List(Of String))
For Each seat As Seat In _seats
seat.Selected = ids.Contains(seat.Id)
Next
RaiseEvent SelectedSeatsChanged()
End Sub
End Class
次に、フォームに次のようなコードを追加して、座席の場所を作成します。
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
SeatingPlan1.Seats.Add(New SeatingPlan.Seat("1A", 3, 3, 20, 20))
SeatingPlan1.Seats.Add(New SeatingPlan.Seat("2A", 26, 3, 20, 20))
SeatingPlan1.Seats.Add(New SeatingPlan.Seat("1B", 3, 26, 20, 20))
SeatingPlan1.Seats.Add(New SeatingPlan.Seat("2B", 26, 26, 20, 20))
End Sub
Private Sub SeatingPlan1_SelectedSeatsChanged() Handles SeatingPlan1.SelectedSeatsChanged
For Each seatId As String In SeatingPlan1.GetSelectedSeatIds
'Do something
Next
End Sub