关于时间网格的建议?

Suggestion for a time grid?

在 VB.NET 中,我希望构建一个 "Time" 网格,非常类似于 Windows 家长部分的时间限制网格:http://www.thinkbroadband.com/images/guides/time-restrictions.png

它需要在单击单元格时在 2 种颜色之间切换

我玩过 One-Cell = One-Label,它有点管用,但是,就像 Windows 时间限制网格一样,如果我移动到标签 同时 按下了左键(不仅是点击标签)。

这是我目前拥有的:

Private Sub ColorToggle(sender As Object, e As MouseEventArgs) Handles Label1.Click, Label2.Click, Label3.Click 'etc..
    If e.Button = Windows.Forms.MouseButtons.Left Then
        sender.backcolor = If(sender.backcolor = SystemColors.Control, Color.LightGreen, SystemColors.Control)
    End If
End Sub

由于当我将鼠标悬停在标签上时发件人保持不变(发件人 = 我最初点击的标签),因此此代码不适用于我的目的。

我正在寻找建议!

谢谢:)

当您点击一个控件并按住鼠标按钮时,该控件会捕获以下鼠标事件,这样您就不会在将鼠标移到其他标签上时从其他标签上获取事件。

诀窍是设置label.Capture = False.

让我们定义颜色:

Private ReadOnly selectedColor As Color = Color.Blue
Private ReadOnly unselectedColor As Color = Color.White

和存储我们当前操作状态的布尔值

Private isSelecting As Boolean = False
Private isUnselecting As Boolean = False

(所有四个都是 class 形式的字段)

现在让我们编写这三个事件处理程序:

Private Sub Label_MouseDown(sender As Object, e As EventArgs)
    'This event starts selecting/unselecting

    Dim label = DirectCast(sender, Label)
    label.Capture = False '<=== THIS IS IMPORTANT!
    If label.BackColor = selectedColor Then
        isUnselecting = True
    Else
        isSelecting = True
    End If
    SelectLabel(label)
End Sub

Private Sub Label_MouseUp(sender As Object, e As EventArgs)
    'This event stops selecting/unselecting

    isSelecting = False
    isUnselecting = False
End Sub

Private Sub Label_MouseEnter(sender As Object, e As EventArgs)
    SelectLabel(DirectCast(sender, Label))
End Sub

我们需要选择或取消选择标签的程序:

Private Sub SelectLabel(label As Label)
    If isSelecting Then
        label.BackColor = selectedColor
    ElseIf isUnselecting Then
        label.BackColor = unselectedColor
    End If
End Sub

就是这样!


脚注:我创建了这样的标签:

Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Const w As Integer = 50, h As Integer = 50

    For x = 1 To 10
        For y = 1 To 10
            Dim lbl As New Label() With {
                .Location = New Point(x * w, y * h),
                .Size = New Size(w, h),
                .BorderStyle = BorderStyle.FixedSingle,
                .BackColor = unselectedColor
            }
            AddHandler lbl.MouseDown, AddressOf Label_MouseDown
            AddHandler lbl.MouseUp, AddressOf Label_MouseUp
            AddHandler lbl.MouseEnter, AddressOf Label_MouseEnter
            Controls.Add(lbl)
        Next
    Next
End Sub

我希望这不是家庭作业...

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
    Dim i As Integer
    With dgv
        .ColumnCount = 0
        .DataSource = Nothing
        .Columns.Add("Day", "Day")
        For i = 0 To 23
            .Columns.Add(i, i)
            .Columns(.Columns.Count - 1).Width = 30
        Next
        For i = 1 To 7
            .Rows.Add({i})
        Next

    End With
End Sub

Private Sub dgv_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.CellClick
    dgv.CurrentCell.Style.BackColor = Color.Blue
End Sub

这里是拖动版本:

Private Sub dgv_MouseUp(sender As Object, e As MouseEventArgs) Handles dgv.MouseUp
    For Each cell As DataGridViewCell In dgv.SelectedCells
        If cell.Style.BackColor = Color.Blue Then
            cell.Style.BackColor = Color.White
        Else
            cell.Style.BackColor = Color.Blue
        End If
    Next
    dgv.ClearSelection()
End Sub