How to make a wxTextCtrl show all lines (read-only)

610 views
Skip to first unread message

Frédéric

unread,
Jul 16, 2018, 10:02:25 AM7/16/18
to wx-u...@googlegroups.com
Hi,

I have a read-only wxTextCtrl with multiple lines. I want that the
wxTextCtrl get the right height to show the whole text. Is there a
mean?
Note that I want a wxTextCtrl and not a wxStaticText because I can
then copy paste from it.

Regards,

F

Igor Korot

unread,
Jul 16, 2018, 5:19:21 PM7/16/18
to wx-u...@googlegroups.com
Hi, Frederic,
Is it some kind of a logger? Are you planning to add the lines to it?

You can try to add the lines to it and then call Layout()...

Thank you.

>
> Regards,
>
> F
>
> --
> Please read http://www.wxwidgets.org/support/mlhowto.htm before posting.
>
> To unsubscribe, send email to wx-users+u...@googlegroups.com
> or visit http://groups.google.com/group/wx-users

Frédéric

unread,
Jul 17, 2018, 12:42:25 AM7/17/18
to wx-u...@googlegroups.com
> Is it some kind of a logger? Are you planning to add the lines to it?

no, the text is fixed and known at construction

> You can try to add the lines to it and then call Layout()...

I will see if this can work. I just doubt that it would work in the
constructor as the object does not have its full with due to sizer not
yet applied.

Thanks,

F

Gunter Königsmann

unread,
Jul 17, 2018, 1:14:19 AM7/17/18
to wx-u...@googlegroups.com
...and you need to tell to use the wxTE_MULTILINE style when creating the wxTextCtrl.

Frédéric

unread,
Jul 17, 2018, 3:35:54 AM7/17/18
to wx-u...@googlegroups.com
> ...and you need to tell to use the wxTE_MULTILINE style when creating the
> wxTextCtrl.

This I do. But call to Layout() did not work.

Gunter Königsmann

unread,
Jul 18, 2018, 1:42:41 PM7/18/18
to wx-users
Did you use a sizer? And is every element a child of the right object?

An example for a wxPanel that does what you want (except of the line that is commented out and that redirects debug messages to the wxTextCtrl) is the following:

wxPanel *wxMaximaFrame::CreateLogPane()
{
wxPanel *panel = new wxPanel(this, -1);
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);

wxTextCtrl *textCtrl = new wxTextCtrl(
panel, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize,
wxTE_MULTILINE | wxTE_READONLY | wxHSCROLL);

vbox->Add(textCtrl, wxSizerFlags().Expand().Proportion(10));

panel->SetSizerAndFit(vbox);
// wxLog::SetActiveTarget(new wxLogTextCtrl(textCtrl));
return panel;
}

Kind regards,
Gunter.

Frédéric

unread,
Jul 19, 2018, 9:18:20 AM7/19/18
to wx-u...@googlegroups.com
> Did you use a sizer? And is every element a child of the right object?

I use a sizer yes, but not a sizer just for the wxTextCtrl.

> An example for a wxPanel that does what you want is the following:
>
> wxPanel *wxMaximaFrame::CreateLogPane() {
> wxPanel *panel = new wxPanel(this, -1);
> wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
>
> wxTextCtrl *textCtrl = new wxTextCtrl(
> panel, -1, wxEmptyString, wxDefaultPosition, wxDefaultSize,
> wxTE_MULTILINE | wxTE_READONLY | wxHSCROLL);
>
> vbox->Add(textCtrl, wxSizerFlags().Expand().Proportion(10));
>
> panel->SetSizerAndFit(vbox);
> return panel;
> }

I tried what you propose but it does not do what I would like: it does
not show the full text. Instead I need to scroll.
I replaced wxHSCROLL by wxTE_NO_VSCROLL but it is not better.

What I would like is just show the whole text in a window that has its
size adjusted to the content.

F

Manolo

unread,
Jul 19, 2018, 12:44:47 PM7/19/18
to wx-users
You can measure the text before the sizer (if any) does.

wxDC has GetMultiLineTextExtent() method for multiline texts and GetTextExtent() for single-line strings.

In other to use the same font as the wxTextCtrl, without the need to pass it to GetXXTextExtend(), you may create a wxWindowDC:

wxWindowDC tdc(myTextCtrl);
wxSize txSize = tdc.GetMultiLineTextExtent(myString);

Reply all
Reply to author
Forward
0 new messages