vb.net DevExperess winforms gridview鼠标按下事件

时间:2015-11-05 09:40:45

标签: vb.net winforms gridview devexpress

我对MouseDown GridControl GridView的{​​{1}}事件有疑问。 如果用户按 control + shift + 单击列标题,则选择该列并单击最左上方将选择所有行。我设法做到了,但任何一个都适用于我的代码。似乎逻辑在if-else语句中存在一些问题。有人可以帮忙吗?

Private Sub gridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles GridView1.MouseDown
    If Control.ModifierKeys = (Keys.Control) Then
        Dim view As GridView = CType(sender, GridView)
        Dim hInfo As GridHitInfo = view.CalcHitInfo(e.Location)
        If hInfo.InColumn Then
            view.ClearSelection()
            SelectCells(hInfo.Column)
        Else
            Return
        End If
        CType(e, DXMouseEventArgs).Handled = True
    ElseIf Control.ModifierKeys = Nothing Then
        Dim view As GridView = CType(sender, GridView)
        view.ClearSelection()
        Return
    Else
        Dim view2 As GridView = CType(sender, GridView)

        Dim hitInfo As GridHitInfo = view2.CalcHitInfo(e.Location)

        If hitInfo.HitTest = GridHitTest.ColumnButton Then
            view2.SelectAll()
        End If
        CType(e, DXMouseEventArgs).Handled = True

    End If

End Sub

Private Sub SelectCells(ByVal column As GridColumn)
    Dim view As GridView = CType(column.View, GridView)

    view.BeginSelection()
    For i As Integer = 0 To column.View.RowCount - 1
        view.SelectCell(i, column)
    Next i
    view.EndSelection()
End Sub

1 个答案:

答案 0 :(得分:1)

您可以使用Select Case语句重新排列您的陈述 这是一个例子:

Private Sub gridView1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles gridView1.MouseDown

    Dim view As GridView = CType(sender, GridView)
    Dim hInfo As GridHitInfo = view.CalcHitInfo(e.Location)

    Select Case True
        Case hInfo.HitTest = GridHitTest.ColumnButton
            view.SelectAll()
        Case Control.ModifierKeys = Nothing
            view.ClearSelection()
            Return
        Case hInfo.InColumn AndAlso Control.ModifierKeys = (Keys.Shift Or Keys.Control)
            view.ClearSelection()
            SelectCells(hInfo.Column)
        Case Else
            Return
    End Select

    CType(e, DXMouseEventArgs).Handled = True

End Sub