Link to home
Start Free TrialLog in
Avatar of jmfairchild
jmfairchild

asked on

MSFlexgrid SelChange event problem

I tried to implement wsh2's code from question:

https://www.experts-exchange.com/questions/10473183/Jump-multiselect-in-MsFlexGrid.html

It works on my work PC (Windows 2000 Pro running Visual Studio Enterprise), but has a problem running on my Home PC (Windows XP/VBPro6). Please offer any comments on this question as the question referenced above is a PAQ, and I will not be able to answer it and award you points there, only here on this question. Thanks.

** - As a bonus, please ensure that once complete, the code will work for Drag & Drop events (that is what I will be doing with it next). If it takes additional code, I will add points. **

Thanks.

- Jim
Avatar of jmfairchild
jmfairchild

ASKER

With apologies to wsh2 if I am doing this wrong, I have pasted the code below to avoid any confusion. Thanks.

---- Start paste ----

The following code works in VB. Please pay particular attention to a couple of things in the code:

1. The LockWindowUpdate API is there to stop screen flicker.. whether or not the API can be ported.. I don't know.

2. Setting Msflexgrid1.HighLight = flexHighlightNever dramatically improves the interface.. allowing only your "selection" coloring to show. (In reading the comments to mcrider above, I think this was more the questioner's problem than mcrider's very worthy code).

3. There is one bug in mcrider's code above (forgive me mc.. <running and ducking>). If the user holds down the Ctrl key and clicks on a cell it will change status. If he then changes his mind.. and Ctrl clicks again.. the cell will NOT revert back to its original status.. as the flexgrid does NOT fire a RowColChange or SelChange event. You have to add code to trap for this.

4. The following code also handles shift key multiselection. Please note.. that the default Msflexgrid's Mouse Drag behaviour is to do a Multiselection of its own and NOT to initiate Drag/Drop operation as we are so accustomed. The code below, disables this functionality.. in lieu of someone wanting to do drag and drop. To enable Mouse Drag multiselection.. just comment out (ie. place a single quote in front of) the following in the MSFlexgrid1_SelChange event procedure:

Private Sub MSFlexGrid1_SelChange()
With MSFlexGrid1

  LockWindowUpdate .hwnd
 
'   If m_booMouseDown _
'   And Not m_booKeyCntrl _
'   And Not m_booKeyShift _
'   Then
'      .Col = .ColSel
'      .Row = .RowSel
'   End If

---------------------------------------
Anyhow.. here is the code. If you have Visual Basic.. load it and run it to see how it works and feels. You can then work on cutting it over to VbScript or whatever.. <smile>.

1. Start a new Standard.Exe project.
2. Add a MsFlexGrid (MSFlexGrid1).
3. Add a Command Button (Command1).
4. Copy/Paste the following code into the Form1 Code Window. Press F5 to run.
5. Press F5 to run. Select some rows as you would in Windows Explorer. Press Delete Rows to delete those rows selected.

<----- Code Begin ----->

Option Explicit

Private Declare Function LockWindowUpdate Lib "user32" _
  (ByVal hwnd As Long) _
  As Long

Private m_lngLastCol As Long
Private m_lngLastColSel As Long
Private m_lngLastRow As Long
Private m_lngLastRowSel As Long
Private m_booKeyCntrl As Boolean
Private m_booKeyShift As Boolean
Private m_booMouseDown As Boolean

Private Sub Command1_Click()
With MSFlexGrid1
 
  LockWindowUpdate .hwnd
 
  Dim lngIdx As Long
  Dim lngRow As Long
  lngRow = .Row
  For lngIdx = .Rows - 1 To .FixedRows Step -1
     .Row = lngIdx
     If .CellBackColor = .BackColorSel _
     Then
        .RemoveItem (lngIdx)
        If lngRow >= lngIdx _
        Then
           lngRow = lngRow - 1
        End If
     End If
  Next lngIdx
  .Row = lngRow

  LockWindowUpdate 0&

End With
End Sub

Private Sub Form_Load()
 
  Command1.Caption = "Delete Row(s)"
 
  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 50
     MSFlexGrid1.AddItem "" + vbTab + Str(intindex) + vbTab + _
     Str(Time()) + vbTab + String(intindex, "*")
  Next intindex

End Sub

Private Sub Form_Resize()
 
  MSFlexGrid1.Move _
     Form1.ScaleWidth * 0.1, _
     Form1.ScaleHeight * 0.1, _
     Form1.ScaleWidth * 0.8, _
     Form1.ScaleHeight * 0.6
 
  Command1.Move _
     Form1.ScaleWidth * 0.1, _
     Form1.ScaleHeight * 0.8, _
     Form1.ScaleWidth * 0.8, _
     Form1.ScaleHeight * 0.1
     
End Sub

Private Sub xMSFlexGrid1_KeyDown(KeyCode As Integer, Shift As Integer)
 
  If Shift = vbShiftMask _
  Then
     m_booKeyShift = True
  End If
  If Shift = vbCtrlMask _
  Then
     m_booKeyCntrl = True
  End If
 
End Sub

Private Sub MSFlexGrid1_Keyup(KeyCode As Integer, Shift As Integer)

  m_booKeyShift = False
  m_booKeyCntrl = False

End Sub

Private Sub MSFlexGrid1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
With MSFlexGrid1

  If Button And vbLeftButton _
  Then
     m_booMouseDown = True
     If Shift = vbShiftMask _
     Then
        .HighLight = flexHighlightAlways
        m_booKeyShift = True
     End If
     If Shift = vbCtrlMask _
     Then
        m_booKeyCntrl = True
     End If
     If .RowSel = m_lngLastRowSel _
     And .ColSel = m_lngLastColSel _
     Then
        Call MSFlexGrid1_SelChange ' Force a Select Change
     End If
  End If

End With
End Sub

Private Sub MSFlexGrid1_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
With MSFlexGrid1
 
  .HighLight = flexHighlightNever
  m_booKeyShift = False
  m_booKeyCntrl = False
  m_booMouseDown = False

End With
End Sub

Private Sub MSFlexGrid1_SelChange()
With MSFlexGrid1

  LockWindowUpdate .hwnd
 
  If m_booMouseDown _
  And Not m_booKeyCntrl _
  And Not m_booKeyShift _
  Then
     .Col = .ColSel
     .Row = .RowSel
  End If
 
  Dim lngCol As Long
  lngCol = .Col
  Dim lngColSel As Long
  lngColSel = .ColSel
  Dim lngRow As Long
  lngRow = .Row
  Dim lngRowSel As Long
  lngRowSel = .RowSel

'  Clear Screen
  If Not m_booKeyCntrl _
  Then
     .Col = .FixedCols
     .Row = .FixedRows
     .ColSel = .Cols - 1
     .RowSel = .Rows - 1
     .CellBackColor = .BackColor
     .CellFontBold = False
     .CellForeColor = .ForeColor
  End If
 
'  Multiline Select
  .Col = .FixedCols
  .Row = lngRow
  .ColSel = .Cols - 1
  .RowSel = lngRowSel
 
  If m_booKeyCntrl _
  And .CellBackColor = .BackColorSel _
  Then
     .CellBackColor = .BackColor
     .CellFontBold = False
     .CellForeColor = .ForeColor
  Else
     .CellBackColor = .BackColorSel
     .CellFontBold = True
     .CellForeColor = .ForeColorSel
  End If
 
  .Col = lngCol
  .ColSel = lngColSel
  .Row = lngRow
  .RowSel = lngRowSel
 
  m_lngLastCol = .Col
  m_lngLastColSel = .ColSel
  m_lngLastRow = .Row
  m_lngLastRowSel = .RowSel
 
  LockWindowUpdate 0&
 
End With
End Sub

<----- Code End ----->

---- End paste ----

- Jim
I have found a solution to the above problem. You can find it at the other question:

https://www.experts-exchange.com/questions/10473183/Jump-multiselect-in-MsFlexGrid.html

- Jim
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America 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