Link to home
Create AccountLog in
Avatar of scbdpm
scbdpmFlag for United States of America

asked on

msFlexGrid- change seleced cell background

I found code here on EE to change the background color of the selected cell(s) of an MSFlexGrid.

However, the code works no matter how many cells are chosen.

I would like to have just one cell per row highlighted/color change.

In other words, my end users will be allowed to select one item per row on the MSFlexGrid and I would like to highlight this item. If they choose another item on that same row, I would like the first cell to change color back to white and then the newly selected cell to change to red, let's say (kind of like radio buttons...)

Avatar of karthika_cts
karthika_cts

You can write the code in "Leavecell" event of the grid. In this event change all the column colors to white and then set the colour for the selected column using a "for" loop

for i=0 to msflexgrid1.cols-1
    msflexgrid1.cell(selectedrow,i).backcolor=vbwhite
next
msflexgrid1.cell(msflexgrid.row,msflexgrid.col)=vbred //or whatever color you like
//msflexgrid.row means the current row
//msflexgrid.col means the current column

OR else
In the "Entercell" event of the Msflexgrid, get column no and row no (as Public variables) and set the color. In the Leave cell event make the previous column as white and current column as red

Note: Currently i don't have Vb installed in my machine and so just given the outline of the code
Avatar of scbdpm

ASKER

appreciate the input but am looking for code that actually works....

am getting errors trying to figure out what you've suggested.
Please try this.

Private Sub Form_Activate()
For i = 1 To MSFlexGrid1.Rows - 1
    For j = 0 To MSFlexGrid1.Cols - 1
    With MSFlexGrid1
                .Row = i
               
    If (i Mod 2) = 0 Then
        .Col = j: .CellBackColor = vbRed   '&HFFFF80
    Else
       .Col = j: .CellBackColor = vbBlue   '&HFFFF80
    End If
    End With
    Next
Next
End Sub

Private Sub Form_Load()
MSFlexGrid1.FixedCols = 0
MSFlexGrid1.Cols = 4
MSFlexGrid1.Rows = 10
MSFlexGrid1.TextMatrix(0, 0) = "Data1"
MSFlexGrid1.TextMatrix(0, 1) = "Data2"
MSFlexGrid1.TextMatrix(0, 2) = "Data3"
MSFlexGrid1.TextMatrix(0, 3) = "Data4"
End Sub

Regards,
M.Raja
Avatar of scbdpm

ASKER

raja_ind82:

this code doesn't work either.

if I try to run wihout breaks, it locks up the app.

if I step trhough, there isn't any change to cell colors.....

to all future posters:

please re-read my initial post. I am looking for solutions that work!
ASKER CERTIFIED SOLUTION
Avatar of raja_ind82
raja_ind82
Flag of India image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of scbdpm

ASKER

raja_ind82:

thank you! that did what I wanted.

Avatar of scbdpm

ASKER

raja_ind82:

I have another question open on MSFlexGrid:
https://www.experts-exchange.com/questions/22858438/msFlexGrid-get-selected-cells-in-each-row.html

can you see if you can help me there too.
I am closing this point