0

我正在 vb.net (WinForms) 中制作座位预订系统,我需要用户能够选择他们希望使用的座位并让它改变颜色(这样他们就可以知道它被选中了)。

我开始尝试使用按钮,但 480 个按钮严重减慢了表单的加载时间。然后我尝试了在行/列中带有按钮的数据网格视图,但无法使其正常工作。

我的问题是,我该怎么做?

是否值得尝试使用 480 个图片框并更改其背景颜色?还是会像 480 按钮一样减慢表单的速度?

4

2 回答 2

1

出于效率考虑,您并不想仅仅创建大量这样的控件。最好制作一个自定义控件,在它自己的单个绘图表面上绘制所有座位。这是一个非常简单的例子:

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
于 2012-05-07T12:49:11.090 回答
0

试试这个:

 private void Form1_Load(object sender, EventArgs e)
        {
            int index;
            Button[] b=new Button[500];
            for(int i=0;i<24;i++)
            for(int j=0;j<20;j++)
            {
                index = (20 * i) + j;
                b[index]=new Button();
                b[index].Text=index.ToString();
                b[index].Location=new Point(j*80,i*30);
                panel1.Controls.Add(b[index]);
                b[index].Click += new EventHandler(ButtonLeft_Click);    
             }

            }

        private void ButtonLeft_Click(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            if (b.BackColor == Color.Black)
                b.BackColor = Color.White;
            else
                b.BackColor = Color.Black;

              //DB Commands here    
        }
于 2012-05-07T12:12:56.513 回答