Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 577
  • Last Modified:

Flexgrid Row Highlighting

Would be grateful if someone could pls provide some working code for the following:

1. whenever a user clicks in an MS Flexgrid, to highlight the entire row (yellow)

2. the value in Column 3 of that row gets shown in a textbox on the form

Thanks!
0
DoctorNash
Asked:
DoctorNash
  • 3
  • 2
1 Solution
 
IeuanJCommented:
The highlight can be set via the properties ForeColorSel & BackColorSel.  Alternatively like this (although this will leave highlighted after selection is removed unless you trap it) -

Private Sub MSFlexGrid1_Click()
  For i = 0 To MSFlexGrid1.Cols - 1
    MSFlexGrid1.BackColor = vbYellow
  Next i
End Sub

The text box can be populated as such -

Private Sub MSFlexGrid1_Click()
  Text1.Text = MSFlexGrid1.TextMatrix(MSFlexGrid1.Row, 1)
End Sub
0
 
DoctorNashAuthor Commented:
leuanJ,
Thanks for your reply. The second part (text box population) works fine, but your 'highlighting' code results in the WHOLE TABLE being painted yellow. Was only looking for the selected row to be highlighted. I resolved that myself, but as you say, now I need code that will remove the previously hightlighted row when a new row is highlighted by the user.
0
 
IeuanJCommented:
Apologies for that, I used the wrong backcolor value, should have been cellbackcolor.  Anyway the below code will return all cellbackcolors to white before highlighting the row you click, this does this every time so creates the illusion of highlighting one row at a time.

Private Sub MSFlexGrid1_Click()
  Dim originalRow As Integer
  Dim originalCol As Integer
  Dim i As Integer
  Dim j As Integer
 
  originalRow = MSFlexGrid1.Row
  originalCol = MSFlexGrid1.Col
 
  For i = 0 To MSFlexGrid1.Rows - 1
    MSFlexGrid1.Row = i
    For j = 0 To MSFlexGrid1.Cols - 1
      MSFlexGrid1.Col = j
      MSFlexGrid1.CellBackColor = vbWhite
    Next j
  Next i

  MSFlexGrid1.Row = originalRow
 
  MSFlexGrid1.BackColor = vbWhite
  For i = 0 To MSFlexGrid1.Cols - 1
    MSFlexGrid1.Col = i
    MSFlexGrid1.CellBackColor = vbYellow
  Next i
 
  MSFlexGrid1.Col = originalCol
End Sub

Remember this will change the properties of any fixedrows & cols as well so if you have these then alter the loops accordingly.

I still think your best option would be to change the backcolorsel value in the flexgrid properties.
0
 
DoctorNashAuthor Commented:
Leuanj,
Brilliant! Works a treat. Your code has actually given me a few other ideas as well, which I played around with last night - you've helped me along the way to getting a better understanding of how this component works. Thanks - here are your well deserved points.
0
 
IeuanJCommented:
your welcome and thanks very much, I've found this object to be a very good and versitile one myself although extrememly poorly documented and not that widely used.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now