Possible bug with e.View.GetCellRectangle in VS2005 Windows Forms

Posted by: j2associates on 8 September 2017, 12:28 pm EST

  • Posted 8 September 2017, 12:28 pm EST

    Hello,


    This is a followup to thread e.View.GetCellRectangle returns empty rectangle: http://www.clubfarpoint.com/Forums/forums/thread/44400.aspx


    I am seeing what may be a bug in VS2005 Windows Forms e.View.GetCellRectangle. I am under a major deadline and do not have time to try this in a test project. Can you reproduce this behavior based on the scenarios described below?


    Back ground information
        Data column 0 Visible = False
        Code is in spread CellClick event
        First 3 Data Columns are frozen
        I need firstDataColumnRectangle because I calculate the last RowHeader column rectangle


        OperationMode = OperationMode.Normal
        SelectionUnit = Model.SelectionUnit.Cell


        AutoGenerateColumns = False
        DataAutoSizeColumns = False


        DefaultStyle.VerticalAlignment = Spread.CellVerticalAlignment.Center


        ' Set column headers to no wrap
        Dim rendererNoWrapText As New Spread.CellType.ColumnHeaderRenderer
        For Each columnHeader As Spread.Column In .ColumnHeader.Columns
            columnHeader.Renderer = rendererNoWrapText
        Next


        DataSource = DataSet
        DataMember = TableName


    '********************** Start of CellClick Event Code ***********************


    Dim rectangle, firstDataColumnRectangle, currentDataColumnRectangle As Drawing.Rectangle
    With e.View


        ' Default with no view ports.
        rectangle = .GetCellRectangle(0, 0, e.Row, e.Column)


        ' First data column rectangle.
        firstDataColumnRectangle = _
            .GetCellRectangle(.GetActiveRowViewportIndex, .GetActiveColumnViewportIndex, e.Row, 0)


        ' Current data column rectangle.
        currentDataColumnRectangle = _
            .GetCellRectangle(.GetActiveRowViewportIndex, .GetActiveColumnViewportIndex, e.Row, e.Column)


    End With


    '********************** End of CellClick Event Code ***********************


    The problem appears to show up after clicking to the right of the frozen columns and then clicking inside the frozen colunmns or within the row header.


    Scenarios to demonstrate the behavior I am seeing


    A. No problem  - Click anywhere within RowHeader or any Frozen column
        rectangle is empty as would be expected because of frozen columns
        firstDataColumnRectangle is correct
        currentDataColumnRectangle is correct


    B. After clicking anywhere within RowHeader or Frozen column


        1. Click a column to the right of the Frozen columns
            rectangle is correct
            firstDataColumnRectangle is correct
            currentDataColumnRectangle is empty - I think this is a bug since firstDataColumnRectangle is correct
       
        2. Click in any other column to the right of the Frozen columns
           The following will be true as long as you click in a column somewhere to the right of the Frozen columns
            rectangle is correct
            firstDataColumnRectangle is empty - I think this is a bug since currentDataColumnRectangle is correct
            currentDataColumnRectangle is correct


        3. Click in the RowHeader
           If you subsequently click again inside the RowHeader or any Frozen column everything resets itself per Scenario A above
            rectangle is empty - I think this is a bug    
            firstDataColumnRectangle is empty - I think this is a bug
            currentDataColumnRectangle is empty - I think this is a bug


    Work-around


    Create a hashtable where Key = e.Column and Value = another hashtable where Key = e.Row and Value = rectangle
    If currentDataColumnRectangle.IsEmpty Then
        ' Try to use rectangle if possible, otherwise try to retrieve value from hash table.
        If rectangle.IsEmpty Then
            With keyHashTable
                If .Contains(e.Column) Then
                    With DirectCast(keyHashTable.Item(e.Column), hashTable)
                        If .Contains(e.row) Then
                            currentDataColumnRectangle = DirectCast(.Item(e.Row), Rectangle)               
                        End If
                    End With
                End If
            End With
        Else
            currentDataColumnRectangle = rectangle
        End If
    End If

  • Replied 8 September 2017, 12:28 pm EST

    The CellClick fires before the active viewport indexes are changed so that the CellClick can be canceled preventing the change. So your first click on the unfrozen area will after clicking in the frozen area will result in the ActiveColumnViewportIndex being that of the frozen viewport not the unfrozen area viewport.

    Instead of using the ActiveColumn/RowViewportIndex to get the clicked on cells viewport use the GetColumnViewportIndexFromX and GetRowViewportIndexFromY methods.
  • Replied 8 September 2017, 12:28 pm EST

    Hello,


    If the ViewPort indices are not updated until after the CellClick event fires, where or in which event should I put my code? I made the changes you suggested above with code inside the CellClick event and got the same results except that in the last case when you click back inside the frozen area the values are correct. Thanks in advance for any ideas and/or suggestions!

  • Replied 8 September 2017, 12:28 pm EST

    Hello,


    When I used your code, I noticed the same results you did. This was because the incorrect viewport was used. I changed the code to what Kevin suggested and it worked correctly for me. One thing to notice is the cell in column 0 is in column viewport -1, since it is a frozen column.

     
    	Private Sub FpSpread1_CellClick(ByVal sender As Object, ByVal e As FarPoint.Win.Spread.CellClickEventArgs) Handles FpSpread1.CellClick
    Dim rectangle, firstDataColumnRectangle, currentDataColumnRectangle As Drawing.Rectangle
    With e.View

    ' Default with no view ports.

    rectangle = .GetCellRectangle(0, 0, e.Row, e.Column)

    ' First data column rectangle.

    firstDataColumnRectangle = _
    .GetCellRectangle(.GetRowViewportIndexFromY(e.Y), -1, e.Row, 0)

    ' Current data column rectangle.

    currentDataColumnRectangle = _
    .GetCellRectangle(.GetRowViewportIndexFromY(e.Y), .GetColumnViewportIndexFromX(e.X), e.Row, e.Column)

    End With


    End Sub
     
  • Replied 8 September 2017, 12:28 pm EST

    Hello,


    I haven't worked with ViewPorts at all so please bear with a couple more questions to make sure I understand this correctly. In the code Scott posted just above, he used -1 for the column viewport index for firstDataColumnRectangle


    1. Does a frozen coumn or row always have a viewport index of -1


    2. If there a column or row property that tells me whether the given column or row is frozen or do I need to calculate it based on the spread's FrozenColumnCount property


    3. Does a non frozen column or row always have a viewport index of 0


    Thanks again for the help!

  • Replied 8 September 2017, 12:28 pm EST

    j2 -

    1) Yes, the frozen columns and rows are always -1 since they are their own viewport.

    2) You'll have to calculate it with the FrozenColumnCount property.

    3) If you haven't added additional viewports then this would be the case.
  • Replied 8 September 2017, 12:28 pm EST

    Thanks Bob!
Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels