zoukankan      html  css  js  c++  java
  • 按住Ctrl可选择多行的DataGrid

    Public Class MyDataGrid
        
    Inherits DataGrid

        
    Private selectedIndices As New ArrayList

        
    Public ReadOnly Property MultiSelectedIndices() As Integer()
            
    Get
                
    Return selectedIndices.ToArray(GetType(Integer))
            
    End Get
        
    End Property

        
    Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
            
    Dim info As DataGrid.HitTestInfo = HitTest(e.X, e.Y)
            
    If HitDataGrid(info) Then
                
    MyBase.OnMouseDown(e)
            
    End If
        
    End Sub

        
    Protected Overrides Sub OnMouseUp(ByVal e As System.Windows.Forms.MouseEventArgs)
            
    MyBase.OnMouseUp(e)

            
    Dim info As DataGrid.HitTestInfo = HitTest(e.X, e.Y)
            
    If info.Type = HitTestType.Cell Then
                
    If selectedIndices.Count = 0 Then
                    
    Me.Select(info.Row)
                
    End If
            
    End If
        
    End Sub

        
    Private Function HitDataGrid(ByVal info As DataGrid.HitTestInfo) As Boolean
            
    Try
                
    Select Case Control.ModifierKeys
                    
    Case Keys.Control
                        
    If info.Row > -1 Then
                            
    If selectedIndices.IndexOf(info.Row) > -1 Then
                                selectedIndices.Remove(info.Row)
                                
    Me.UnSelect(info.Row)
                            
    Else
                                selectedIndices.Add(info.Row)
                                
    Me.Select(info.Row)
                            
    End If
                        
    End If
                        
    Return False
                    
    Case Keys.Shift
                        
    If info.Row > -1 Then
                            
    For Each IndexOld As Integer In selectedIndices
                                
    Me.UnSelect(IndexOld)
                            
    Next
                            selectedIndices.Clear()
                            
    Dim i, intStep As Integer
                            
    If info.Row > Me.CurrentRowIndex Then
                                intStep 
    = 1
                            
    Else
                                intStep 
    = -1
                            
    End If
                            
    For i = Me.CurrentRowIndex To info.Row Step intStep
                                selectedIndices.Add(i)
                                
    Me.Select(i)
                            
    Next
                        
    End If
                        
    Return False
                    
    Case Else
                        
    For Each index As Integer In selectedIndices
                            
    Me.UnSelect(index)
                        
    Next
                        selectedIndices.Clear()
                        
    If info.Type = DataGrid.HitTestType.RowHeader Then
                            selectedIndices.Add(info.Row)
                        
    End If
                        
    Return True
                
    End Select
            
    Catch ex As Exception
                Debug.WriteLine(ex.ToString)
            
    End Try
        
    End Function

    End Class
  • 相关阅读:
    提权函数之RtlAdjustPrivilege()
    用C#写外挂或辅助工具必须要的WindowsAPI
    ASP.net中保持页面中滚动条状态
    asp.net窗体的打开和关闭
    界面原型设计工具 Balsamiq Mockups
    在List(T)中查找数据的两种方法
    P2158 [SDOI2008]仪仗队 题解
    P1531 I Hate It 题解
    C#
    破解网站防盗链
  • 原文地址:https://www.cnblogs.com/LeoWong/p/1357115.html
Copyright © 2011-2022 走看看