How do I restore ViewPorts when using FrozenColumns

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

  • Posted 8 September 2017, 12:48 pm EST

    Hello,


    I am saving ViewPort settings at form close as user settings and restoring them when the form is opened. We have a couple frozen columns on the FpSpread and I haven't been able to get the displayed columns to match the saved columns in this case. Even though you can get the Right column for a given view port, there is no corresponding set right column that I can see.


    In the attached Form Close and Form Open screen shots, note the position of the ScrollBox in the Horizontal ScrollBar. Thanks in advance for any ideas and/or suggestions!


     


    2007/09/FormSettings.JPG
  • Replied 8 September 2017, 12:48 pm EST

    j2 -

    I don't believe it's a bug.  I think it's a timing issue.  Try moving the LoadSettings() call back to Form_Load and suspending the painting of the spread until the LoadSettings() is finished processing.  This worked for me as well...

    FpSpread1.SuspendLayout()
    LoadSettings()
    FpSpread1.ResumeLayout()

    You can test that this is not a bug by putting a button on your form and calling the ShowColumn method...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    FpSpread1.ShowColumn(0, 13, HorizontalPosition.Right)
    End Sub
  • Replied 8 September 2017, 12:48 pm EST

    j2 -

    Try moving the call to LoadSettings() to the FormActivated event.  I think it is just a timing issue.  Worked for me.
  • Replied 8 September 2017, 12:48 pm EST

    Hello Bob,


    That seems like it should work but no matter how I set things up, it is not displaying properly. I have attached a small project showing the behavior I am seeing.


    2007/09/ViewPort_FrozenColumnIssue.zip
  • Replied 8 September 2017, 12:48 pm EST

    j2 -

    Have you tried using the ShowColumn method?
  • Replied 8 September 2017, 12:48 pm EST

    Hello Bob,


    That works but is not the correct event because the Activated event gets called multiple times. The only way to make that work would be to save my settings when the form is Deactivated. User preference settings should be available to be saved and restored when the form is closed and created.


    I need this to be called once, when the form is opened which is why I used the Load event. I also tried putting it in the VisibleChanged event and that didn't work either. Is this a bug?

  • Replied 8 September 2017, 12:48 pm EST

    Hey Bob, no problem. This isn't a show stopper at the moment. I just wanted to make sure I understood your response.
  • Replied 8 September 2017, 12:48 pm EST

    Thanks, Bob! It's still not working for me. Attached is a new zip file with Suspend and Resume Layout as you suggested above. I am using the latest maintenance release, as far as I know.


    2007/09/ViewPort_FrozenColumnIssue-2.zip
  • Replied 8 September 2017, 12:48 pm EST

    j2 -

    I'm not sure what's happening here.  I'll see if Scott has any ideas.
  • Replied 8 September 2017, 12:48 pm EST

    Does your response mean that when you run the attached project that you don't see the same thing?

  • Replied 8 September 2017, 12:48 pm EST

    Thank you Scott!


    Here is the code I used to change the number of FrozenColumns:

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    Try
    With Me.FpSpread1
    Dim list As New ArrayList
    Dim index As Integer

    ' Save view port left column values because they are reset by FrozenColumn.
    For index = 0 To .GetColumnViewportCount - 1
    list.Add(.GetViewportLeftColumn(index))
    Next

    .ActiveSheet.FrozenColumnCount = Me.ComboBox1.SelectedIndex

    '
    Restoer view port left column values after changing FrozenColumn property.
    For index = 0 To .GetColumnViewportCount - 1
    .SetViewportLeftColumn(index, Integer.Parse(list(index).ToString))
    Next
    End With
    Catch ex As Exception
    Throw
    End Try
    End Sub 'ComboBox1_SelectedIndexChanged
    It appears that when setting the FrozenColumn property as above that it freezes the columns
    in ViewPort(0) but then hides the FrozenColumn in other ViewPorts. Is my understanding correct?

     
  • Replied 8 September 2017, 12:48 pm EST

    Hello,


    The problem is due to the order the code is being set. You need to set the FrozenCOlumnCount property first, since that will automatically scroll the Spread viewport back to the left when you set that property. Also, there is not a need to Call the ShowColumn method.

     
        Private Sub LoadSettings()
    Try
    With
    My.Settings
    Me.ComboBox1.SelectedIndex = .FrozenColumns

    Dim value As String = .ViewPortSettings
    If value IsNot Nothing AndAlso value.Trim.Length > 0 Then
    ' Strip trailing delimiter.

    Dim pattern As String = Regex.Escape(";" & ";")
    Dim viewPorts() As String = Regex.Split(value, pattern, RegexOptions.IgnoreCase)
    If viewPorts(viewPorts.Length - 1).Trim.Length = 0 Then ReDim Preserve viewPorts(viewPorts.GetUpperBound(0) - 1)
    With Me.FpSpread1
    For index As Integer = 0 To viewPorts.Length - 1
    Dim viewPortInfo() As String = viewPorts(index).Trim.Split(CChar(";"))
    ' Not interested in adding view port and setting left most column for first item.

    If index > 0 Then
    .AddViewport(-1, index)
    End If
    .SetViewportLeftColumn(index, Integer.Parse(viewPortInfo(0).ToString))
    ' .ShowColumn(index, Integer.Parse(viewPortInfo(1).ToString), HorizontalPosition.Right)

    .SetViewportPreferredWidth(index, Integer.Parse(viewPortInfo(2).ToString))
    Debug.WriteLine(viewPortInfo(0) & " " & viewPortInfo(1))
    Next
    End With
    End If
    End With
    Catch
    ex As Exception
    Throw
    End Try
    End Sub
    'LoadSettings

     
  • Replied 8 September 2017, 12:48 pm EST

    I do see the same thing as you, but at times it seems to work.  I'd like Scott to have a look at it.
  • Replied 8 September 2017, 12:48 pm EST

    Hello,


    That is correct. The frozen rows/columns are always only in the top left viewport.

Need extra support?

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

Learn More

Forum Channels