How do I make a break line in a textctrl?

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
MegAmaNeo1
In need of some credit
In need of some credit
Posts: 6
Joined: Sun Sep 20, 2020 4:44 pm

How do I make a break line in a textctrl?

Post by MegAmaNeo1 » Sun Sep 20, 2020 4:54 pm

Hello,

I have a TextCtrl that is supposed to display some text where I need multiple lines / a break line command.
I am a beginner and looked for many threads but didn't found many and often don't understand it.
What I have is something like this:

string test = "hi";
string test2 = "second line";

TextCtrl1->SetValue(test);

Normally I would have used something like "<< endl" but unfortunately this doesnt work here.

Which command do I use to make a new line?

catalin
Moderator
Moderator
Posts: 1594
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: How do I make a break line in a textctrl?

Post by catalin » Sun Sep 20, 2020 5:29 pm

First of all, are you using wxTextCtrl or something else?
For the wx component, did you create it as a multi-line control? It has AppendText(), and operator<<(), so you should really be fine with it.

MegAmaNeo1
In need of some credit
In need of some credit
Posts: 6
Joined: Sun Sep 20, 2020 4:44 pm

Re: How do I make a break line in a textctrl?

Post by MegAmaNeo1 » Sun Sep 20, 2020 5:35 pm

Correct, I am using a wxTextCtrl.

I did not create it as a multiline control but read about "wxTE_MULTILINE" in here: https://docs.wxwidgets.org/3.0/classwx_text_ctrl.html

But how do I create it "as a multi-line control"? Totally confused.

catalin
Moderator
Moderator
Posts: 1594
Joined: Wed Nov 12, 2008 7:23 am
Location: Romania

Re: How do I make a break line in a textctrl?

Post by catalin » Sun Sep 20, 2020 5:41 pm

One of the parameters of the constructor is style. wxTE_MULTILINE is one of the possible styles.

MegAmaNeo1
In need of some credit
In need of some credit
Posts: 6
Joined: Sun Sep 20, 2020 4:44 pm

Re: How do I make a break line in a textctrl?

Post by MegAmaNeo1 » Sun Sep 20, 2020 6:40 pm

I already learned someting from your answer, but I think I need to get more detailed:

I have a button in my GUI. When I push it, I want the wxTextCtrl to print out a text with multiple lines.
I found out that by using the following commands I can use the simple std::cout:

#include <iostream>
wxTextCtrl *control = new wxTextCtrl(...);
std::streambuf *sbOld = std::cout.rdbuf();
std::cout.rdbuf(control);
// use cout as usual, the output appears in the text control
...
std::cout.rdbuf(sbOld);

In my project I have now the code:

void DeutschprojektFrame::OnButton1Click1(wxCommandEvent& event)
{
wxTextCtrl *control = new wxTextCtrl(...);
std::streambuf *sbOld = std::cout.rdbuf();
std::cout.rdbuf(control);

cout << "test" << endl << "test2";
std::cout.rdbuf(sbOld);
}


Here I inserted the above code into the button section of my code.
However he returns an error message saying " 'control' was not declared in this scope"

User avatar
doublemax
Moderator
Moderator
Posts: 15159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How do I make a break line in a textctrl?

Post by doublemax » Sun Sep 20, 2020 8:06 pm

Code: Select all

void DeutschprojektFrame::OnButton1Click1(wxCommandEvent& event)
{
wxTextCtrl *control = new wxTextCtrl(...);
std::streambuf *sbOld = std::cout.rdbuf();
std::cout.rdbuf(control);

cout << "test" << endl << "test2";
std::cout.rdbuf(sbOld);
}
That code doesn't make any sense, you probably don't want to create a new wxTextCtrl each time the user clicks the button. When the wxTextCtrl is created, its pointer should be stored in a member variable. I think there is an option for that in wxSmith, but as i don't use it, i can't tell you exactly where.

Then, assuming the pointer is stored in a member variable "TextCtrl1", you can write text to it like this:

Code: Select all

void DeutschprojektFrame::OnButton1Click1(wxCommandEvent& event)
{
  TextCtrl1->WriteText("more text\r\nsecond line\r\n");
}
WriteText() inserts the text at the current cursor position.
If you want to add text at the end, use AppendText().
If you want to replace the old content completely, use SetValue()
Use the source, Luke!

Post Reply