?
Solved

MSFlexgrid SelChange event problem

Posted on 2003-03-14
3
Medium Priority
?
1,288 Views
Last Modified: 2013-12-26
I tried to implement wsh2's code from question:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_10473183.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
0
Comment
Question by:jmfairchild
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 4

Author Comment

by:jmfairchild
ID: 8139772
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
0
 
LVL 4

Author Comment

by:jmfairchild
ID: 8141117
I have found a solution to the above problem. You can find it at the other question:

http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Controls/Q_10473183.html

- Jim
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 8141157
Points refunded and placed in PAQ

Computer101
E-E Admin
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

801 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