[RESOLVED] [MSFlexgrid] How to multi-select rows with mouse's left click + [Ctrl] key?
Dear all,
I have a msflexgrid. One of the column is "amount".
I want to know, is there any codes that i can multi-select the rows in the grid by clicking the left button of mouse while the [Ctrl] key is being pressing, then, there is a textbox that display the sum of the "amount" column of the selected row. (The behavior is just like "MS Excel")
Thank you a lot for your help in advance :wave:
Re: [MSFlexgrid] How to multi-select rows with mouse's left click + [Ctrl] key?
If you set SelectionMode property to flexSelectionByRow then you will be able to select rows using SHIFT+ARROW keys or SHIFT+MOUSE.
MSFlexGrid1.SelectionMode = flexSelectionByRow
Re: [MSFlexgrid] How to multi-select rows with mouse's left click + [Ctrl] key?
Quote:
Originally Posted by
RhinoBull
If you set SelectionMode property to flexSelectionByRow then you will be able to select rows using SHIFT+ARROW keys or SHIFT+MOUSE.
MSFlexGrid1.SelectionMode = flexSelectionByRow
thanks. :wave:
one more question: how to know which rows the user has selected and then calculated the sum of amount immediately and reflect this amount in a textbox?
Re: [MSFlexgrid] How to multi-select rows with mouse's left click + [Ctrl] key?
Hi, further question:
I am not sure if "MSFlexGrid1.SelectionMode = flexSelectionByRo" equals to what i expect.
What i want is, in case the msflexgrid has 10 rows, I can randomly select Row 1, 4 and 5 only by clicking "mouse's left button" while the [Ctrl] key is being pressed. (not selecting Rows 1 to 5)
And there is a textbox outside the msflexgrid, this textbox will display the sum of the amount column in the rows that user has selected.
Thank you.
Re: [MSFlexgrid] How to multi-select rows with mouse's left click + [Ctrl] key?
Quote:
I can randomly select Row 1, 4 and 5 only by clicking "mouse's left button" while the [Ctrl] key is being pressed. (not selecting Rows 1 to 5)
Try this.
Place MSFlexGrid1 on a new form and simply run the form...
Code:
Option Explicit
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwnd As Long) _
As Long
Private m_booKeyCtrl As Boolean, m_booKeyShift As Boolean
Private Sub Form_Load()
Form1.Move Screen.Width * 0.1, Screen.Height * 0.1, _
Screen.Width * 0.8, Screen.Height * 0.8
With MSFlexGrid1
.AllowUserResizing = flexResizeColumns
.AllowBigSelection = False
.Cols = 4
.ColWidth(0) = 400
.FillStyle = flexFillRepeat
.FocusRect = flexFocusNone
.HighLight = flexHighlightNever
.Rows = 1
.SelectionMode = flexSelectionFree
Dim lngIndex As Long
For lngIndex = 1 To .Cols - 1
.ColWidth(lngIndex) = 1500
Next lngIndex
End With
Dim intIndex As Integer
For intIndex = 1 To 500
MSFlexGrid1.AddItem "" & vbTab & "x" & Format(intIndex, "000") & vbTab & _
Str(Time()) & vbTab & String(intIndex, "*")
Next intIndex
End Sub
Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
If Shift = vbShiftMask Then m_booKeyShift = True
If Shift = vbCtrlMask Then m_booKeyCtrl = True
End Sub
Private Sub MSFlexGrid1_Keyup(KeyCode As Integer, Shift As Integer)
With MSFlexGrid1
m_booKeyCtrl = False
m_booKeyShift = False
End With
End Sub
Private Sub MSFlexGrid1_RowColChange()
With MSFlexGrid1
Static booBusy As Boolean
If m_booKeyShift Or booBusy Then Exit Sub
booBusy = True
Dim intThisCol As Integer, intThisRow As Integer
intThisCol = .Col
intThisRow = .Row
LockWindowUpdate .hwnd
If m_booKeyCtrl Then
.Col = 1
.Row = intThisRow
.ColSel = .Cols - 1
.RowSel = intThisRow
If .CellBackColor = .BackColorSel Then
.CellBackColor = .BackColor
.CellForeColor = .ForeColor
Else
.CellBackColor = .BackColorSel
.CellForeColor = .ForeColorSel
End If
Else
.Col = 1
.Row = 1
.ColSel = .Cols - 1
.RowSel = .Rows - 1
.FillStyle = flexFillRepeat
.CellBackColor = .BackColor
.CellForeColor = .ForeColor
.Col = 1
.Row = intThisRow
.ColSel = .Cols - 1
.RowSel = intThisRow
.CellBackColor = .BackColorSel
.CellForeColor = .ForeColorSel
End If
.Col = intThisCol
.Row = intThisRow
LockWindowUpdate 0&
booBusy = False
End With
End Sub
Private Sub MSFlexGrid1_SelChange()
With MSFlexGrid1
If Not m_booKeyShift Then Exit Sub
Dim intThisCol As Integer, intThisRow As Integer
intThisCol = .Col
intThisRow = .Row
Dim intNextCol As Integer, intNextRow As Integer
intNextCol = .ColSel
intNextRow = .RowSel
LockWindowUpdate .hwnd
'~~> Clear Screen
.Col = 1
.Row = 1
.ColSel = .Cols - 1
.RowSel = .Rows - 1
.FillStyle = flexFillRepeat
.CellBackColor = .BackColor
.CellForeColor = .ForeColor
'~~> Update Multiline
.Col = 1
.Row = intThisRow
.ColSel = .Cols - 1
.RowSel = intNextRow
.FillStyle = flexFillRepeat
.CellBackColor = .BackColorSel
.CellForeColor = .ForeColorSel
LockWindowUpdate 0&
Tag900:
.Col = intNextCol
.Row = intNextRow
End With
End Sub
Re: [MSFlexgrid] How to multi-select rows with mouse's left click + [Ctrl] key?
thanks, all :)
Koolsid, so, how about sum() of an amount column of the selected row immediately in a textbox or label that outside the datagrid?
I have added the red line code, it seems the codes should be added there.. but i dont know how to reset the value if user de-select a row..
Quote:
Private Sub MSFlexGrid1_RowColChange()
With MSFlexGrid1
Static booBusy As Boolean
If m_booKeyShift Or booBusy Then Exit Sub
booBusy = True
Dim intThisCol As Integer, intThisRow As Integer
intThisCol = .Col
intThisRow = .Row
'Assume column 5 is the amount field
g_total = g_total + .TextMatrix(intThisRow, 5)
Label1.Caption = g_total
Re: [MSFlexgrid] How to multi-select rows with mouse's left click + [Ctrl] key?
Quote:
how about sum() of an amount column of the selected row immediately in a textbox or label that outside the datagrid?
Hint:
1) Try and see which event is fired when you click on the row
2) If the row was not highlighted earlier then get the row number and once you get that, extract the value from the respective column
3) Add it to the textbox value...
4) If the row was highlighted earlier then get the row number and once you get that, extract the value from the respective column
5) Deduct it from the textbox value...
Give it a try. If you get stuck, post the code that you have tried and we will definitely help you :)