views:

75

answers:

1

I'm working with a MSFlexGrid control in VB6, but I'm also having some problems retrieving the ColPos property for merged columns. The grid that I've generated looks something like this:

-----------------------------
|         8/17/2010         |
-----------------------------
|   Column 1  |  Column 2   |
-----------------------------

The first row is fixed and the two columns are merged, so both columns contain 8/17/2010 in the first row.

During the Click event, I'm positioning a text box over a cell in the second row, and when I set its Left and Top properties using the FlexGrid's ColPos and RowPos properties, I end up with the textbox positioned over column 1. This happens even if I clicked in column 2.

I've checked the Col property, and it's correctly set to 2 after clicking in the second column, but ColPos(1) and ColPos(2) both return the same value, which is the distance from column 1's left edge to the left edge of the control.

When merging is disabled on the flexgrid, the problem goes away, but I'd rather leave it on since it makes the grid a bit more readable.

Is there any way to retrieve the correct position of the selected column when another cell in the column is merged with another one, or do I need to calculate the column position manually?

A: 

Haven't found a way to do it through the control, but here's a function that I put together that does the trick. It counts backward from the selected cell until it reaches the leftmost cell it's merged with, and then adds the widths of the merged cells to the first one's position. Probably could be cleaned up some, but this does work.

Private Function RealColPos(Col As Integer, grid as MSFlexGrid)

    With grid

        Dim i As Integer, merged As Integer

        i = Col - 1: merged = 0
        Do While .ColPos(Col) = .ColPos(i)
            merged = merged + 1
            i = i - 1
        Loop

        If merged > 0 Then
            RealColPos = .ColPos(Col - merged)
            Do While merged > 0
                RealColPos = RealColPos + .ColWidth(Col - merged)
                merged = merged - 1
            Loop
        Else
            RealColPos = .ColPos(Col)
        End If

    End With

End Function
derekerdmann