Link to home
Start Free TrialLog in
Avatar of scmmicro
scmmicro

asked on

Multiple Selection in MSFlexgrid

Which property of MSFlexgrid allows the user to select multiple rows for processing?
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi scmmicro,

You can only select consecutive rows by setting the .Row and .RowSel properties. You cannot by default select non-consecutive rows unless you manage all of this yourself.

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
Hi!

First set AllowBigSelection = True which will allow a selection by single click on a row or a column.
Set SelectionMode to 1,2 or 3 to allow selection of only rows, only columns, or both.

After that you can use properties row, col, rowsel and colsel to determine what is selected

Try this example:
Place Msflexgrid and 4 textboxes to a form and paste this code.
Run it and try selecting something.
----------------------------------------------------------------------------------
Private Sub Form_Load()
    MSFlexGrid1.Cols = 10
    MSFlexGrid1.Rows = 10
End Sub

Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    Text1.Text = "Row: " & MSFlexGrid1.Row
    Text2.Text = "RowSel: " & MSFlexGrid1.RowSel
    Text3.Text = "Col: " & MSFlexGrid1.Col
    Text4.Text = "ColSel: " & MSFlexGrid1.ColSel
End Sub
--------------------------------------------------------------


If you wish to select non-consecutive, then you could use an array which will hold an information for each row (if it is selected or not).

Try example from:    http://webwarper.net/ww/~GZ/www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_20803581.html      
to see a possible approach.
Avatar of scmmicro
scmmicro

ASKER

Can u send me the sample code for the same (using .row and .rowsel properties) ?
ASKER CERTIFIED SOLUTION
Avatar of dbrckovi
dbrckovi
Flag of Croatia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is my library code for managing row selection.  It requires use of a fixed column in the grid.

Private m_IsOnFixedPart As Boolean
Private m_IsZeroRow As Boolean

Private Sub oGrid_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)

    ' Are we on fixed column?  If so, highlight row(s)
   
    With oGrid
        If .Rows = 0 And .Cols = 0 Then
            Exit Sub
        End If
       
        ' After deselecting rows, need these to get back to original cell
        Dim lRowOriginal As Long: lRowOriginal = .MouseRow
        Dim lColOriginal As Long: lColOriginal = .MouseCol
   
        ' Don't understand this, the code was downloaded.
        Dim l As Long
        Dim lWidth As Long
        For l = 0 To .Cols - 1
            If .ColIsVisible(l) Then
                lWidth = lWidth + .ColWidth(l)
            End If
        Next
       
        If .RowHeightMin = 0 Then
            m_IsOnFixedPart = (x < .ColWidth(0)) Or _
                       (x > lWidth) Or _
                       (y < .RowHeight(0))
        Else
            m_IsOnFixedPart = (x < .ColWidth(0)) Or _
                       (x > lWidth) Or _
                       (y < .RowHeight(0)) Or _
                       (y > .Rows * .RowHeightMin)
        End If
                       
        m_IsZeroRow = (.RowHeight(0) >= y)
       
       
        '' Highlighting
        If Not m_IsOnFixedPart Or m_IsZeroRow Then
            DeselectAllRows
            .Row = lRowOriginal
            .Col = lColOriginal
        Else
            If (Shift And vbShiftMask) = vbShiftMask Then
                SelectContigious 'Select from the current row to the clicked row
            ElseIf (Shift And vbCtrlMask) = vbCtrlMask Then
                ToggleSelect  'Toggle the select in the clicked row
            Else
                 DeselectAllRows
               
                'select clicked row
                 .RowSel = .Row
                 .ColSel = .Cols - 1
                .CellBackColor = vbHighlight
                .CellForeColor = vbHighlightText

            End If  '' If (Shift And vbShiftMask) = vbShiftMask Then
        End If  '' If Not m_IsOnFixedPart Or m_IsZeroRow Then
    End With
End Sub


Private Sub DeselectAllRows()
   
    Dim lRowOriginal As Long
    With oGrid
        lRowOriginal = .Row
        .Row = .FixedRows
        .Col = .FixedCols
        .RowSel = .Rows - 1
        .ColSel = .Cols - 1
        .FillStyle = flexFillRepeat
       
        .HighLight = flexHighlightNever
        .CellBackColor = .BackColor
        .CellForeColor = .ForeColor
        .Row = lRowOriginal
        .ColSel = .Cols - 1
    End With

End Sub

Private Sub SelectContigious()
'Select from the current row to the clicked row
    Dim lRow As Long
    Dim lMouseRow As Long

    With oGrid
        lRow = .Row
        lMouseRow = .MouseRow
        .FillStyle = flexFillRepeat
        .Row = lRow
        .Col = .FixedCols
        .RowSel = lMouseRow
        .ColSel = .Cols - 1
        .HighLight = flexHighlightNever
        .CellBackColor = vbHighlight
        .CellForeColor = vbHighlightText
    End With
End Sub

Private Sub ToggleSelect()
 'Toggle the select in the clicked row
    With oGrid
        .Row = .MouseRow
        .FillStyle = flexFillRepeat
        .Col = .FixedCols
       
        .ColSel = .Cols - 1
        .HighLight = flexHighlightNever
        If .CellBackColor = vbHighlight Then
            .CellBackColor = .BackColor
            .CellForeColor = .ForeColor
        Else
            .CellBackColor = vbHighlight
            .CellForeColor = vbHighlightText
        End If
    End With
End Sub
Thanks
answer I expected is not  this.  when i click  first row . first row selects.  when i click second row, both of first row and second row are selected. my requirement is when i click second row, second row should be  selected only. first row should not be selected
please , I want proper answer for this question