[Webinar] Streamline your web hosting managementRegister Today

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

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?
0
ackid32
Asked:
ackid32
  • 5
  • 4
  • 2
1 Solution
 
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
 
TimCotteeCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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