编写vb.net代码,使得在listbox里选中一个时间后(例如13:00-15:00),该时段被标注为不可再选,即不能被他人再次选择。(要求不用dictionary语句)
时间: 2024-03-08 16:47:22 浏览: 23
可以通过在listbox中存储时间段的起始时间和结束时间,以及一个标志位来实现该功能。具体步骤如下:
1. 在listbox中添加时间段,每个时间段需要存储起始时间、结束时间和标志位。可以使用自定义类来封装这些信息,也可以使用字符串来存储,多个信息之间用分隔符隔开。
2. 当用户选中一个时间段时,遍历listbox中所有时间段,找到与选中的时间段重合的时间段,将其标志位置为不可选。
3. 当用户取消选中一个时间段时,遍历listbox中所有时间段,找到与取消选中的时间段重合的时间段,将其标志位置为可选。
4. 在listbox的SelectedIndexChanged事件中执行上述操作即可。
下面是一个示例代码:
```
'自定义时间段类
Public Class TimeSlot
Private _startTime As DateTime
Private _endTime As DateTime
Private _selected As Boolean
Public Property StartTime() As DateTime
Get
Return _startTime
End Get
Set(ByVal value As DateTime)
_startTime = value
End Set
End Property
Public Property EndTime() As DateTime
Get
Return _endTime
End Get
Set(ByVal value As DateTime)
_endTime = value
End Set
End Property
Public Property Selected() As Boolean
Get
Return _selected
End Get
Set(ByVal value As Boolean)
_selected = value
End Set
End Property
Public Sub New(ByVal startTime As DateTime, ByVal endTime As DateTime)
_startTime = startTime
_endTime = endTime
_selected = False
End Sub
Public Overrides Function ToString() As String
Return String.Format("{0}-{1}", _startTime.ToString("HH:mm"), _endTime.ToString("HH:mm"))
End Function
End Class
'添加时间段到listbox
Private Sub AddTimeSlots()
Dim timeSlots As New List(Of TimeSlot)()
timeSlots.Add(New TimeSlot(DateTime.Parse("08:00"), DateTime.Parse("10:00")))
timeSlots.Add(New TimeSlot(DateTime.Parse("10:00"), DateTime.Parse("12:00")))
timeSlots.Add(New TimeSlot(DateTime.Parse("13:00"), DateTime.Parse("15:00")))
timeSlots.Add(New TimeSlot(DateTime.Parse("15:00"), DateTime.Parse("17:00")))
For Each ts As TimeSlot In timeSlots
lstTimeSlots.Items.Add(ts)
Next
End Sub
'标记已选中的时间段为不可选
Private Sub MarkSelectedTimeSlots()
Dim selectedTimeSlot As TimeSlot = TryCast(lstTimeSlots.SelectedItem, TimeSlot)
If selectedTimeSlot IsNot Nothing Then
For Each ts As TimeSlot In lstTimeSlots.Items
If ts IsNot selectedTimeSlot AndAlso ts.StartTime <= selectedTimeSlot.EndTime AndAlso ts.EndTime >= selectedTimeSlot.StartTime Then
ts.Selected = True
End If
Next
End If
End Sub
'标记取消选中的时间段为可选
Private Sub UnmarkSelectedTimeSlots()
Dim unselectedTimeSlot As TimeSlot = TryCast(lstTimeSlots.SelectedItem, TimeSlot)
If unselectedTimeSlot IsNot Nothing Then
For Each ts As TimeSlot In lstTimeSlots.Items
If ts IsNot unselectedTimeSlot AndAlso ts.StartTime <= unselectedTimeSlot.EndTime AndAlso ts.EndTime >= unselectedTimeSlot.StartTime Then
ts.Selected = False
End If
Next
End If
End Sub
'处理listbox的SelectedIndexChanged事件
Private Sub lstTimeSlots_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles lstTimeSlots.SelectedIndexChanged
UnmarkSelectedTimeSlots()
MarkSelectedTimeSlots()
End Sub
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)