Solved

ctrl key and MS Flex Grid

Posted on 2003-11-10
4
492 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:pigface
  • 3
4 Comments
 
LVL 5

Expert Comment

by:g0rath
Comment Utility
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
0
 
LVL 5

Expert Comment

by:g0rath
Comment Utility
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.
0
 

Author Comment

by:pigface
Comment Utility
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.

0
 
LVL 5

Accepted Solution

by:
g0rath earned 250 total points
Comment Utility
This example will only highligh rows if you hold down the CTRL key. When you release the CTRL key the highligh event no longer occurs.

If you hit the CTRL key again it resets the graphical highlighting, but your right there is a bug in that function

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
+      ReDim MultiSelectArray(0)
    End If
End Sub

Add the line with the + by it

Follow the events.

The Click event can only occur if the MultipleSelect is true, and that only becomes true when you fire the KeyDown Event, and it gets released on KeyUp

If you try to step through this via the IDE using 'F8' it doesn't occur properly as the IDE causes some events of its own.

When you can do the MouseClick the row that you have selected gets highlighted, now you will need to write logic to not add the same row if you click the same row multiple times...but I was hoping to give you enough of a skeleton to go and modify it to your needs.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now