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?
ackid32Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adityauCommented:
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
0
ackid32Author Commented:
no!, read the question again, i want two different colors in the same row.
0
TimCotteeHead of Software ServicesCommented:
Use the following code to highlight a row when the grid is clicked.

There are a couple of problems with adityau's suggestion, firstly if you change the .col or .row property within the rowcolchange event you actually trigger another rowcolchange event unless you also add some code to make sure that this does not happen. In anything more complicated than the code given this may cause recursion, which will give unpredictable results and possible lead to stack overflow errors. Secondly it is inadvisable to use the constant vbBlack in cells as VB doesn't handle it properly, it is always better to use the value 1 to represent black when setting cell colours.

Private Sub MSFlexGrid1_Click()
    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)
            Next
        Next
        .Row = intCurRow 'Restore position
        .Col = intCurCol
        .Redraw = True
    End With
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

TimCotteeHead of Software ServicesCommented:
Sorry ackid32, I failed to read the question properly myself, I will rehash it slightly and repost. Apologies again!
0
TimCotteeHead of Software ServicesCommented:
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
0
adityauCommented:
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.
0
TimCotteeHead of Software ServicesCommented:
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


0
adityauCommented:
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.
0
TimCotteeHead of Software ServicesCommented:
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
0
adityauCommented:
Yes boss. It works. Thanx.
0
ackid32Author Commented:
fine, thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.