Link to home
Start Free TrialLog in
Avatar of pigface
pigfaceFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ctrl key and MS Flex Grid

I have screen that uses flex grid and need to do the following three things

select a single row and assign and action from a combo box (this is working)

select multiple rows using shift and assign and action from a combo box to these rows (this is working using row and rowsel)

select multiple rows using CTRL and assign and action from a combo box to these rows - this one is not working and I am unsure of how to tackle this.  Does anyone have any code that can determine what rows have been highlighted via the ctrl key and perform an action against these rows?

Thanks.
Avatar of g0rath
g0rath

VERSION 5.00
Object = "{5E9E78A0-531B-11CF-91F6-C2863C385E30}#1.0#0"; "MSFLXGRD.OCX"
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   5520
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6675
   LinkTopic       =   "Form1"
   ScaleHeight     =   5520
   ScaleWidth      =   6675
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton Command2
      Caption         =   "Reset"
      Height          =   495
      Left            =   3720
      TabIndex        =   3
      Top             =   3840
      Width           =   1575
   End
   Begin VB.ComboBox Combo1
      Height          =   315
      Left            =   720
      TabIndex        =   2
      Text            =   "Testing"
      Top             =   4440
      Width           =   3975
   End
   Begin VB.CommandButton Command1
      Caption         =   "Populate Data"
      Height          =   975
      Left            =   1920
      TabIndex        =   1
      Top             =   480
      Width           =   2415
   End
   Begin MSFlexGridLib.MSFlexGrid MSFlexGrid1
      Height          =   1695
      Left            =   960
      TabIndex        =   0
      Top             =   1800
      Width           =   4575
      _ExtentX        =   8070
      _ExtentY        =   2990
      _Version        =   393216
      Cols            =   3
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public MultipleSelect As Boolean
Private MultiSelectArray() As Integer
Public MultiIndex As Integer

Private Sub PrintValues()
    Dim i As Integer
    Dim msg As String
    msg = ""
    For i = 0 To UBound(MultiSelectArray) - 1
        msg = msg & " " & MultiSelectArray(i)
    Next i
    MsgBox msg
    ' At this point we have all the selections that you chose
    ' do whatever
End Sub
Private Sub Combo1_Click()
    PrintValues
End Sub

Private Sub Command1_Click()
    MSFlexGrid1.AddItem "1" & vbTab & "A" & vbTab & "100"
    MSFlexGrid1.AddItem "2" & vbTab & "B" & vbTab & "100"
    MSFlexGrid1.AddItem "3" & vbTab & "C" & vbTab & "100"
    MSFlexGrid1.AddItem "4" & vbTab & "D" & vbTab & "100"
    MSFlexGrid1.AddItem "5" & vbTab & "E" & vbTab & "100"
    MSFlexGrid1.AddItem "6" & vbTab & "F" & vbTab & "100"
    MSFlexGrid1.AddItem "7" & vbTab & "G" & vbTab & "100"
    MSFlexGrid1.AddItem "8" & vbTab & "H" & vbTab & "100"
    MSFlexGrid1.AddItem "9" & vbTab & "I" & vbTab & "100"
   
    MSFlexGrid1.Row = 5
    MSFlexGrid1.CellFontBold = True
   
    Combo1.AddItem "1"
    Combo1.AddItem "2"
    Combo1.AddItem "3"
End Sub
Private Sub DeselectAllRows()

    Dim lRowOriginal As Long
    With MSFlexGrid1
        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 SelectMultiple()

    Dim lRowOriginal As Long

    Debug.Print "----------"
    Debug.Print MSFlexGrid1.MouseRow
    Debug.Print MSFlexGrid1.Row
    With MSFlexGrid1
        .Col = .FixedCols
        .RowSel = .Row
        .ColSel = .Cols - 1
        .CellBackColor = vbHighlight
        .CellForeColor = vbHighlightText
        ReDim Preserve MultiSelectArray(UBound(MultiSelectArray) + 1)
        MultiSelectArray(UBound(MultiSelectArray) - 1) = .Row
        Debug.Print MultiSelectArray(UBound(MultiSelectArray) - 1)
    End With
End Sub

Private Sub Command2_Click()
    ReDim MultiSelectArray(0)
    DeselectAllRows
End Sub

Private Sub Form_Load()
    ReDim MultiSelectArray(0)
End Sub

Private Sub MSFlexGrid1_Click()
    If MultipleSelect Then
        SelectMultiple
    End If
End Sub

Private Sub MSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
    ' And Not to stop multiple iterations of this event firing
    If KeyCode = 17 And Not MultipleSelect Then
        Debug.Print "KeyDown firing again"
        MultipleSelect = True
        DeselectAllRows
    End If
End Sub

Private Sub MSFlexGrid1_KeyUp(KeyCode As Integer, Shift As Integer)
    If KeyCode = 17 Then
        MultipleSelect = False
    End If
End Sub
Here's an example of it working....multiple graphical selections, and also saving it to an array...then just reading the rows from the array when you hit your combo box event.
Avatar of pigface

ASKER

This works, but on the assumption that the user click ctrl everytime that they want a row rather than keeping ctrl pressed and seecting several rows.  By keep ctrl down and selecting several rows, only the last row is recorded when the ctrl is released.

ASKER CERTIFIED SOLUTION
Avatar of g0rath
g0rath

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