VB RadioButton转换为Array

时间:2015-09-22 14:23:08

标签: arrays vb.net radio-button

尝试制作航空公司预订计划,用户输入其姓名,然后通过单选按钮选择座位。

一旦他们保留座位,该程序应该重置,所选座位不再可供下一个人选择他们的座位。所以这就是我感到困惑的事情。

当下一个人选择座位时,如何让以前“保留”的无线电按钮改变颜色而不能选择。最终飞机将填满,没有更多的用户可以选择。

如果需要上下文或澄清,我很乐意添加更多信息。

提前谢谢。

2 个答案:

答案 0 :(得分:0)

Private Sub UpdateSeat()
    For i As Integer = 0 To 3
        If arrSeatAvailableFirst(i) = False Then
            arrSeatFirst(i).BackColor = Color.DarkRed
            arrSeatFirst(i).Enabled = False


        End If

然后在按钮的Click事件

Private Sub btnReserve_Click(sender As Object, e As EventArgs) Handles   btnReserve.Click
    For i As Integer = 0 To 3
        If arrSeatFirst(i).Checked Then
            arrSeatAvailableFirst(i) = False
            radSelected = arrSeatFirst(i).Text
        Else
            arrSeatAvailableFirst(i) = True
        End If
   End Sub
End Class

答案 1 :(得分:0)

以下内容可帮助您顺利上路。只需将其直接粘贴到名为AirplaneSeats的空白WinForm应用程序中;一切都在运行时添加。设置断点并逐步完成所有步骤以查看详细信息。

Option Strict On
Option Explicit On

Imports System.Windows.Forms


Public Class AirplaneSeats
    Private numberOfSeats As Integer = 30I
    Private SeatLayoutPanel As New FlowLayoutPanel With {.FlowDirection = FlowDirection.LeftToRight, .AutoScroll = True, .Dock = DockStyle.Fill}
    Private TableLayoutPanel1 As New TableLayoutPanel With {.Dock = DockStyle.Fill, .ColumnCount = 1, .RowCount = 2}
    Private SubmitBtn As New Button With {.Text = "Submit", .Anchor = AnchorStyles.Right}
    Private reservedSeats As New List(Of Int32)


    Private Sub AirplaneSeats_Load(sender As Object, e As EventArgs) Handles Me.Load
        AddHandler Me.SubmitBtn.Click, AddressOf SubmitBtnClicked

        For i As Integer = 1I To numberOfSeats
            Dim seatRadio As New RadioButton With {.Text = "Seat " & i, .Tag = i, .Checked = False, .Enabled = True, .AutoCheck = True}
            AddHandler seatRadio.Click, AddressOf seatButtonClicked
            Me.SeatLayoutPanel.Controls.Add(seatRadio)
        Next

        Me.TableLayoutPanel1.ColumnStyles.Add(New ColumnStyle(SizeType.Percent, 100.0!))
        Me.TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Percent, 100.0!))
        Me.TableLayoutPanel1.RowStyles.Add(New RowStyle())
        Me.TableLayoutPanel1.Controls.Add(Me.SeatLayoutPanel, 0I, 0I)
        Me.TableLayoutPanel1.Controls.Add(Me.SubmitBtn, 0I, 1I)
        Me.Controls.Add(Me.TableLayoutPanel1)
    End Sub

    Private Sub seatButtonClicked(sender As Object, e As EventArgs)
        Dim selectedSeat As RadioButton
        If TypeOf sender Is RadioButton Then selectedSeat = DirectCast(sender, RadioButton) Else Exit Sub

        For Each ctrl As Control In Me.SeatLayoutPanel.Controls
            If TypeOf ctrl Is RadioButton Then
                Dim ctrlRadio As RadioButton = DirectCast(ctrl, RadioButton)
                If CInt(ctrl.Tag) <> CInt(selectedSeat.Tag) Then
                    RemoveHandler ctrlRadio.Click, AddressOf Me.seatButtonClicked
                    ctrlRadio.Checked = False
                    AddHandler ctrlRadio.Click, AddressOf seatButtonClicked
                End If
            End If
        Next
    End Sub

    Private Sub SetSeatAvailability()
        For Each ctrl As Control In Me.SeatLayoutPanel.Controls
            If Me.reservedSeats.Contains(CInt(ctrl.Tag)) Then ctrl.Enabled = False
        Next
    End Sub

    Private Sub SubmitBtnClicked(sender As Object, e As EventArgs)
        For Each ctrl As Control In Me.SeatLayoutPanel.Controls
            If TypeOf ctrl Is RadioButton Then
                Dim ctrlRadio As RadioButton = DirectCast(ctrl, RadioButton)
                If ctrlRadio.Checked Then
                    Me.reservedSeats.Add(CInt(ctrlRadio.Tag))
                    ctrlRadio.Checked = False
                    Exit For
                End If
            End If
        Next
        SetSeatAvailability()
    End Sub
End Class