如何使用Winforms在vb.net中制作大型的按钮网格(24x20或类似的网格)?
我正在vb.net(WinForms)中建立一个座位预定系统,我需要用户能够选择他们想要使用的座位并为其更改颜色(以便他们可以告诉它已选择)。
I'm making a seat booking system in vb.net (WinForms) and I need the user to be able to select the seat they wish to use and for it to change colour (so they can tell that it selected).
我开始尝试使用按钮,但是480个按钮严重降低了表单的加载时间。然后,我尝试了在行/列中带有按钮的数据网格视图,但无法使其正常工作。
I began to try using buttons, but 480 buttons seriously slowed down the load time for the form. I then tried a data grid view with buttons in the rows/columns, but couldnt make that work properly.
我的问题是,我该怎么做?
My question is, how can I do this?
使用480个图片框并更改其背景颜色是否值得?还是只是以与480个按钮相同的方式降低表单的速度?
Would it be worth trying to use 480 picture boxes and change their background colour? Or would that just slow the form down the same way as 480 buttons?
出于效率考虑,您并不是真的想要只创建大量这样的控件。最好使用单个自定义控件在其自己的单个绘图表面上绘制所有座位。这是一个非常简单的示例:
For efficiency sake, you don't really want to just create a ton of controls like that. It would be better to make a single custom control that draws all the seats on it's own single drawing surface. Here's a very simple example:
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
然后,在您的表单中,输入一些代码li为此,可以创建座椅的位置:
Then, in your form, put some code like this to create the locations of the seats:
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