2

I have a wxTextCtrl with many lines of text with scrollbars enabled. Inside an event, I'd like to scroll to the end of the control and redraw the control.

Here's what I have:

    def event_scroll_to_end(self, event):
        self.m_textCtrl1.SetScrollPos(
            wx.VERTICAL,
            self.m_textCtrl1.GetScrollRange(wx.VERTICAL))
        event.Skip()

This scrolls to the end and updates/redraws the scroll bar itself, but it doesn't update the textCtrl, which still shows scrolled to its current position.

How can I actually scroll the textCtrl as well so that the contents are scrolled to the end, as the scrollbar indicates?

devtk
  • 1,999
  • 2
  • 18
  • 24

3 Answers3

1

I suspect that you need to set the insertion point, if you want to be positioned at the end of the text i.e.

def event_scroll_to_end(self, event):
    self.m_textCtrl1.SetScrollPos(
        wx.VERTICAL,
        self.m_textCtrl1.GetScrollRange(wx.VERTICAL))
    self.m_textCtrl1.SetInsertionPoint(-1)
    event.Skip()

Use SetInsertionPoint(0) to position yourself at the start of the text.

Rolf of Saxony
  • 21,661
  • 5
  • 39
  • 60
  • That moved the insertion point (cursor) to the end, but did not scroll the control at all. – devtk Mar 25 '19 at 13:33
  • @devtk Whether `ShowPosition(LastPosition)` or `SetInsertionPoint(-1)` are used the net result is the same, by definition, the text is scrolled to the bottom. If that was not what you wanted, please clarify in your question. – Rolf of Saxony Mar 25 '19 at 15:59
  • That is exactly what I wanted, but using either of those commands didn't scroll the window for me. They did move the insertion point, but they didn't scroll the box such that the insertion point was visible. Maybe a functionality change due to a version difference? I'm on '4.0.4 msw (phoenix) wxWidgets 3.0.5' on Win10. – devtk Mar 25 '19 at 17:43
  • Now that I retry it, it is working. Probably my error during the initial testing. – devtk Mar 25 '19 at 17:49
1

The ShowPosition function can be used to scroll to the end by showing the last position of the buffer.

    def event_scroll_to_end(self, event):
        self.m_textCtrl3.ShowPosition(self.m_textCtrl3.GetLastPosition())
        event.Skip()
devtk
  • 1,999
  • 2
  • 18
  • 24
-1

I have been struggling with this as well. In the end, the following worked for me:

    mywindow.SetInsertionPoint(-1)
    mywindow.ShowPosition(mywindow.GetLastPosition())
    mywindow.Refresh()
    mywindow.Update()