Growing TextCtrl

Hello

How can I make a TextCtrl grow as the user type, avoiding scroll bars?

I tried using styles (wx.TE_NO_VSCROLL), size functions (".GetBestFittingSize()") and manually calculating it’s size (len(text.Value) * 8 + 10), but soon realised that styles don’t affect controls size, BestSize functions don’t bother if there is a scroll bar or not and it’s not as easy to calculate a non-fixed-width font size.

The current code is:


self.entry = wx.TextCtrl(self, wx.NewId(), style=wx.TE_MULTILINE)
self.Bind(wx.EVT_TEXT, self.RefreshSize)

def RefreshSize(self, event):
    #how do I resize it properly?
    self.Fit()
    self.Layout()

Hi Lucas,

How can I make a TextCtrl grow as the user type, avoiding scroll bars?

I tried using styles (wx.TE_NO_VSCROLL), size functions
(".GetBestFittingSize()") and manually calculating it's size
(len(text.Value) * 8 + 10), but soon realised that styles don't affect
controls size, BestSize functions don't bother if there is a scroll bar or
not and it's not as easy to calculate a non-fixed-width font size.

You might try to do something like this (not really tested...):

1) Bind the wx.EVT_KEY_UP event for the text control:

yourTextCtrl.Bind(wx.EVT_KEY_UP, self.OnKeyUp)

2) Calculate the text size yourself, like this (assuming "self" is
your text control):

    def OnKeyUp(self, event):
        """Handles the wx.EVT_KEY_UP event for the text control."""

            # auto-grow the textctrl:
            mySize = self.GetSize()

            dc = wx.ClientDC(self)
            sx, sy, dummy = dc.GetMultiLineTextExtent(self.GetValue() + "M")

            if sx > mySize.x:
                self.SetSize((sx, -1))

        event.Skip()

HTH.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On Thu, Jan 8, 2009 at 2:55 PM, Lucas Boppre Niehues wrote:

Hi Lucas,

Hi Lucas,

How can I make a TextCtrl grow as the user type, avoiding scroll bars?

I tried using styles (wx.TE_NO_VSCROLL), size functions
(".GetBestFittingSize()") and manually calculating it's size
(len(text.Value) * 8 + 10), but soon realised that styles don't affect
controls size, BestSize functions don't bother if there is a scroll bar or
not and it's not as easy to calculate a non-fixed-width font size.

You might try to do something like this (not really tested...):

1) Bind the wx.EVT_KEY_UP event for the text control:

yourTextCtrl.Bind(wx.EVT_KEY_UP, self.OnKeyUp)

2) Calculate the text size yourself, like this (assuming "self" is
your text control):

   def OnKeyUp(self, event):
       """Handles the wx.EVT_KEY_UP event for the text control."""

           # auto-grow the textctrl:
           mySize = self.GetSize()

           dc = wx.ClientDC(self)
           sx, sy, dummy = dc.GetMultiLineTextExtent(self.GetValue() + "M")

           if sx > mySize.x:
               self.SetSize((sx, -1))

       event.Skip()

Sorry, I didn't get the fact that you do not want *vertical*
scrollbars, I understood that you wanted it to grow in the horizontal
direction... In you want to avoid vertical scrollabrs, I suggest you
to use the ExpandoTextCtrl in wx.lib:

from wx.lib.expando import ExpandoTextCtrl

And you can use it as a standard text control.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

···

On Thu, Jan 8, 2009 at 3:01 PM, Andrea Gavana wrote:

On Thu, Jan 8, 2009 at 2:55 PM, Lucas Boppre Niehues wrote:

Andrea, you gave me a double answer. Yes, I want horizontal grow too, the wx.TE_NO_VSCROLL was just an example.

The solution for horizontal grow worked quite well, but I found that using wx.EVT_TEXT the grow was smoother.

The ExpandoTextCtrl worked perfectly.

Thank you