Dynamically creating a text field. Topic is solved
Dynamically creating a text field.
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>.
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>.
Re: Dynamically creating a text field.
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.
However, I would just keep track of the added controls myself, it seems easier and less error-prone.
Re: Dynamically creating a text field.
Thanks. How to keep track of added controls which are randomly added?
Re: Dynamically creating a text field.
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 wrote:Thanks. How to keep track of added controls which are randomly added?
Re: Dynamically creating a text field.
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?
Re: Dynamically creating a text field.
As I wrote above, I would just store pointers to the controls upon creating them.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?
Re: Dynamically creating a text field.
To dynamically remove the text field, I'm deleting its sizer which holds it.
I suppose this is the right way.
But I am getting a runtime error for it because of this:
Am I setting it correctly?
Code: Select all
m_Sizer->Clear(true);
But I am getting a runtime error for it because of this:
Code: Select all
wxBoxSizer *m_Sizer = (wxBoxSizer*)event.GetEventObject();
Code: Select all
m_button->Bind(wxEVT_BUTTON, &MyDialog::OnRemoveTextField, this, -1, -1, bSizer2);
Re: Dynamically creating a text field.
I believe not, according to the documentation, the user data object should be retrieved with GetEventUserData(), NOT GetEventObject().purplex88 wrote:But I am getting a runtime error for it because of this:Am I setting it correctly?Code: Select all
wxBoxSizer *m_Sizer = (wxBoxSizer*)event.GetEventObject();
Code: Select all
m_button->Bind(wxEVT_BUTTON, &MyDialog::OnRemoveTextField, this, -1, -1, bSizer2);
Re: Dynamically creating a text field.
It's now this error:
I have never used this GetEventUserData() method before. Sure not GetClientData()?
I have never used this GetEventUserData() method before. Sure not GetClientData()?
Re: Dynamically creating a text field.
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....
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....
Re: Dynamically creating a text field.
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 ?
Should I use SetClientData, SetClientObject or EventUserData ?
Re: Dynamically creating a text field.
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...
Re: Dynamically creating a text field.
Okay, thanks. Everything works but I have other questions which I guess I should ask in new thread.