在工程中添加一个类模块,名为 Selection
然后加入以下代码:
Dim colRows As Collection
Dim ControlKey As Boolean
Public WithEvents flx As MSFlexGrid
Public Sub ToggleSelection()
Dim x As Integer
Dim d
Dim mode As Boolean
Dim OldRow, OldCol
x = flx.Row
If x = 0 Then Exit Sub
On Error Resume Next
OldRow = flx.Row
OldCol = flx.col
d = colRows(CStr(x))
If Err Then ' not add
colRows.Add x, CStr(x)
mode = True
Else
colRows.Remove CStr(x) 'remove
mode = False
End If
MakeSelectedStatus x, mode
flx.Row = OldRow
flx.col = OldCol
End Sub
Private Sub MakeSelectedStatus(Row As Integer, mode As Boolean)
Dim col
Dim Fcolor As Long, BColor As Long
If mode Then
Fcolor = flx.ForeColorSel
BColor = flx.BackColorSel
Else
Fcolor = vbBlack
BColor = vbWhite
End If
With flx
.Visible = False
.Row = Row
For col = 1 To .Cols - 1
.col = col
.CellBackColor = BColor
.CellForeColor = Fcolor
Next col
.Visible = True
End With
End Sub
Public Sub ClearSelection()
'
Dim x
Dim OldRow, OldCol
With flx
OldRow = .Row
OldCol = .col
For Each x In colRows
MakeSelectedStatus CInt(x), False
Next
.Row = OldRow
.col = OldCol
End With
End Sub
Public Property Get SelectedRow() As Collection
Set SelectedRow = colRows
End Property
Private Sub Class_Initialize()
Set colRows = New Collection
End Sub
Private Sub Class_Terminate()
Set colRows = Nothing
End Sub
Private Sub flx_KeyDown(KeyCode As Integer, Shift As Integer)
'
If KeyCode = 17 Then ControlKey = True
End Sub
Private Sub flx_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = 17 Then ControlKey = False
End Sub
Private Sub flx_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
'
If Button <> 1 Then Exit Sub
If Not ControlKey Then
ClearSelection
Else
ToggleSelection
End If
End Sub
在你使用以前,请声明
Dim mSelection as New Selection
...
Set mSelection.Flx=MSFlexGrid1
VB MSHFLEXGRID MSHFLEXGRID 同时选中不连续的多行
最新推荐文章于 2021-12-10 17:36:38 发布