• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1678
  • Last Modified:

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...)

0
scbdpm
Asked:
scbdpm
  • 4
  • 2
1 Solution
 
karthika_ctsCommented:
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
0
 
scbdpmAuthor Commented:
appreciate the input but am looking for code that actually works....

am getting errors trying to figure out what you've suggested.
0
 
raja_ind82Commented:
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
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
scbdpmAuthor Commented:
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!
0
 
raja_ind82Commented:
yes, sorry i misunderstood the question. Please check this code.

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

Private Sub MSFlexGrid1_Click()

    RowSelect = MSFlexGrid1.RowSel
    ColSelect = MSFlexGrid1.ColSel

    With MSFlexGrid1
        .Row = RowSelect
    For j = 0 To MSFlexGrid1.Cols - 1
   
    If j = ColSelect Then
        .Col = ColSelect: .CellBackColor = vbRed    '&HFFFF80
    Else
       .Col = j: .CellBackColor = vbWhite    '&HFFFF80
    End If
    Next
    End With
End Sub

you can select single item in multiple row and can't select multiple item in single row.

Regards,
M.Raja
0
 
scbdpmAuthor Commented:
raja_ind82:

thank you! that did what I wanted.

0
 
scbdpmAuthor Commented:
raja_ind82:

I have another question open on MSFlexGrid:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_22858438.html

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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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