Link to home
Start Free TrialLog in
Avatar of ackid32
ackid32

asked on

msflexgrid colors

i have 4 cols and want to show the current row in a different color as well as the current column by another color how?
Avatar of adityau
adityau

This I could do for only row. Code is given below.

Private Sub MSFlexGrid1_RowColChange()
    With MSFlexGrid1
        .Col = 1
        .ColSel = .Cols - 1
        .BackColorSel = vbBlack
    End With
End Sub
Avatar of ackid32

ASKER

no!, read the question again, i want two different colors in the same row.
ASKER CERTIFIED SOLUTION
Avatar of TimCottee
TimCottee
Flag of United Kingdom of Great Britain and Northern Ireland 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
Sorry ackid32, I failed to read the question properly myself, I will rehash it slightly and repost. Apologies again!
Add the following three lines in the loop after the colour settings.

If intRow <> intCurRow Then
  .CellBackColor = IIf(intCol = intCurCol, vbRed, vbWhite) 'Set Colours
  .CellForeColor = IIf(intCol = intCurCol, vbYellow, 1)
End If
This code works only when the user selects the cell using mouse. The user can even select the cell using arrow keys. At that time it does not work. If you write it in LeaveCell or EnterSel or RowColChange events, it will go into an infinte loop.
I agree, that it will go into an infinite loop if you place it in any event which is fired when a row or column changes. The only way to prevent this from happening is to define a boolean variable at the form level, which is set to true when you first fire the row/col change event for example. Then in the first line of code in the event procedure you need to check for the value being true or false and proceed accordingly. E.g.,

(Declarations Section)
Private blnDisable As Boolean

(RowColChange)

  IF blnDisable Then Exit Sub
  blnDisable = True
  .... ' Existing code snippet
  blnDisable = False
End Sub


It can't be done. Because whenever the cell changes, again the same event gets fired and even though you use a boolean variable won't be helpful.

If u do it in Click event, when the user selects another row using arrow key, it won't work.
adityau, try it, create a form with a flexgrid, any number of rows / columns. Paste the following. It works whether you use the mouse or the keyboard to move around the grid, it does not send the app into an infinite loop either!

Private blnDisable As Boolean

Private Sub MSFlexGrid1_RowColChange()
    If blnDisable Then Exit Sub
    blnDisable = True
    With MSFlexGrid1
        intCurRow = .Row 'Save current position
        intCurCol = .Col
        .Redraw = False 'Turn off repainting till we are done
        For intRow = 1 To .Rows - 1 'Iterate rows
            .Row = intRow
            For intCol = 1 To .Cols - 1 'Iterate columns
                .Col = intCol
                .CellBackColor = IIf(intRow = intCurRow, vbBlue, vbWhite) 'Set Colours
                .CellForeColor = IIf(intRow = intCurRow, vbWhite, 1)
                If intRow <> intCurRow Then
                    .CellBackColor = IIf(intCol = intCurCol, vbRed, vbWhite) 'Set Colours
                    .CellForeColor = IIf(intCol = intCurCol, vbYellow, 1)
                End If
            Next
        Next
        .Row = intCurRow 'Restore position
        .Col = intCurCol
        .Redraw = True
    End With
    blnDisable = False
End Sub
Yes boss. It works. Thanx.
Avatar of ackid32

ASKER

fine, thanks.