对时间网格的建议?

时间:2015-06-22 19:53:14

标签: vb.net time colors grid

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

需要在单击鼠标时在两种颜色之间切换

我玩过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

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

我正在寻找建议!

谢谢:)

2 个答案:

答案 0 :(得分:1)

当您单击控件并按住鼠标按钮时,此控件会捕获以下鼠标事件,以便在将鼠标移动到其他标签上时不会从其他标签中获取事件。

诀窍是设置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

(所有四个作为表单类的字段)

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

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

答案 1 :(得分:1)

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

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