我正在制作酒店预订系统。我有15个按钮代表每个房间。一旦选择了日期,如果房间已被预订,那么我需要带有数字的按钮变为红色且无法选择。但是,我还需要按钮为红色,时间在日期和日期之间。
这是我到目前为止的代码。我不确定从哪里开始。
Dim date1 As DateTime = dtpDateIn.Value.Date
Dim date2 As DateTime = dtpDateOut.Value.Date
Dim da As OleDbDataAdapter = New OleDbDataAdapter
Dim BookingFound As String = False
MyConn = New OleDbConnection
MyConn.ConnectionString = connString
MyConn.Open()
str1 = ("Select * from BookingInformation where [Date In] >= '" & date1 & "' AND [Date Out] <= '" & date2 & "'")
'("SELECT * FROM [BookingInformation] WHERE [Date In] = #" & dtpDateIn.Value.Date & "#")
Dim cmd1 As OleDbCommand = New OleDbCommand(str1, MyConn)
dr = cmd1.ExecuteReader
While dr.Read()
BookingFound = True
strDateOut = dr("Date Out").ToString
strDateIn = dr("Date In").ToString
strRoomNumber = dr("Room Number").ToString
CmbRooms.Items.Remove(strRoomNumber)
End While
MyConn.Close()
End Sub
答案 0 :(得分:0)
首先,如果您将所有房间按钮收集到一个列表中并按房间号排序,那将会更容易一些。理想情况下,按钮名称将类似于btnRoom01。如果确保任何单个数字的数字都以零开头,那么我的样本中使用的排序方法将起作用。如果没有,您需要根据自己的要求重新编写它。
创建表单范围列表 -
Dim roomButtons As New List(Of Button)
这里是收集按钮的代码
Private Sub AddButtonsToCollection()
roomButtons.Clear()
For Each ctl As Control In Me.Controls
Dim btn As Button
If ctl.GetType = GetType(Button) Then
btn = CType(ctl, Button)
If btn.Name.Contains("btnRoom") Then
roomButtons.Add(btn)
roomButtons = roomButtons.OrderBy(Function(x) x.Name).ToList
End If
End If
Next
End Sub
你的while循环看起来应该是这样的
While dr.Read()
Dim bookingDateIn, bookingDateOut As Date
BookingFound = True
Date.TryParse(dr("Date Out").ToString, bookingDateIn)
Date.TryParse(dr("Date In").ToString, bookingDateIn)
Dim RoomNumber As Integer = CInt(dr("Room Number").ToString)
If (bookingDateIn <= date2) And (bookingDateOut >= date1) Then
roomButtons(RoomNumber - 1).BackColor = Color.Red
roomButtons(RoomNumber - 1).Text = RoomNumber.ToString & vbCrLf & bookingDateIn.ToShortDateString & vbCrLf & bookingDateOut.ToShortDateString
End If
End While