Dynamically creating a text field. Topic is solved

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
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Dynamically creating a text field.

Post by purplex88 »

I want to dynamically create a text field when '+' sign is clicked next to it.

How would I go iterate all the text fields in the dialog box since I won't know their IDs and such? Probably there's a way to enumerate all and knowing they exist so I can get the data into vector<string>.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Dynamically creating a text field.

Post by PB »

You can iterate over a list returned by GetChildren, casting the children to wxTextCtrl and retrieving the text from those where the cast succeeded.

However, I would just keep track of the added controls myself, it seems easier and less error-prone.
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: Dynamically creating a text field.

Post by purplex88 »

Thanks. How to keep track of added controls which are randomly added?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Dynamically creating a text field.

Post by PB »

purplex88 wrote:Thanks. How to keep track of added controls which are randomly added?
I do not understand "randomly added". There must be a code adding those controls and this code should be modified to not only add the control to the dialog but add it also to the internal list (e.g., std::vector<wxTextCtrl*> of such controls.
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: Dynamically creating a text field.

Post by purplex88 »

Yeah, makes sense to have a vector there. Do I need the ID of each or ANY id can work since I have the handle already?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Dynamically creating a text field.

Post by PB »

purplex88 wrote:Yeah, makes sense to have a vector there. Do I need the ID of each or ANY id can work since I have the handle already?
As I wrote above, I would just store pointers to the controls upon creating them.
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: Dynamically creating a text field.

Post by purplex88 »

To dynamically remove the text field, I'm deleting its sizer which holds it.

Code: Select all

m_Sizer->Clear(true);
I suppose this is the right way.

But I am getting a runtime error for it because of this:

Code: Select all

wxBoxSizer *m_Sizer = (wxBoxSizer*)event.GetEventObject();
Am I setting it correctly?

Code: Select all

m_button->Bind(wxEVT_BUTTON, &MyDialog::OnRemoveTextField, this, -1, -1, bSizer2);
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Dynamically creating a text field.

Post by PB »

purplex88 wrote:But I am getting a runtime error for it because of this:

Code: Select all

wxBoxSizer *m_Sizer = (wxBoxSizer*)event.GetEventObject();
Am I setting it correctly?

Code: Select all

m_button->Bind(wxEVT_BUTTON, &MyDialog::OnRemoveTextField, this, -1, -1, bSizer2);
I believe not, according to the documentation, the user data object should be retrieved with GetEventUserData(), NOT GetEventObject().
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: Dynamically creating a text field.

Post by purplex88 »

It's now this error:
getuserdata.png
getuserdata.png (39.25 KiB) Viewed 2193 times
I have never used this GetEventUserData() method before. Sure not GetClientData()?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Dynamically creating a text field.

Post by PB »

I am sorry I was wrong, you cannot do it like this.

While GetEventUserData() works, wxWidgets takes ownership of the user data object passed to Bind() and destroys it upon disconnecting the handler or on program termination. This of course messes things up.

You need to change the code, I would just store the pointer to the sizer in a member variable but this depends on your code....
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: Dynamically creating a text field.

Post by purplex88 »

That means I can't store handles to objects in UserData but can I at least keep an ID of Sizer? That information is necessary to let me know which sizer to delete as I click [minus] button to remove the textfield.

Should I use SetClientData, SetClientObject or EventUserData ?
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Dynamically creating a text field.

Post by PB »

Sorry, I do not understand. I would do as I wrote before, but of course it depends on your code, e.g. on how many sizers you need to keep track of...
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: Dynamically creating a text field.

Post by purplex88 »

Okay, thanks. Everything works but I have other questions which I guess I should ask in new thread.
Post Reply