im building a basic GUI for a college project, im using wxwidgets and codeblocks because c++ is my comfort programming languaje. However, I want to modify the options on a wxChoice because those options are not meant to be always the same, that means that if I append something to the choice list and then i want to update the list to a complete different list, first i have to remove the old items from the choice list and then append the new ones, but i cant find how to do that. Here's my code:
//Event
void TestFrame::OnChoice1Select(wxCommandEvent& event)
{
fstream in;
string str,str2;
int i;
//File where I store my list of lists
in.open("Recursos/Carreras-LC.txt",ios::in);
//Go to an specific line of the file where the list for the current choice is
in=gotoLine(in,Choice1->GetSelection()+1);
//Gets the line with the list
getline(in,str,'\n');
in.close();
//Here is where I'll put the code to remove the current list of choices
//My code to append the items from the list i got before
i=0;
while(str[i]!='\0'){
if(str[i]==','){
Choice2->Append(_(str2));
i++;
str2="";
}else{
str2+=str[i];
i++;
}
}
}
Also if there's a better way to do this kind of dynamic GUI please tell me. thanks in advance.
i have to remove the old items from the choice list and then append the new ones, but i cant find how to do that
You can use the wxChoice::Clear() method to remove all entries from a choice control, and you can use the wxChoice::Delete(unsigned int n) method to remove a specific entry from the control.
They're listed on the documentation page under the "Public Member Functions inherited from wxItemContainer" section.
Also if there's a better way to do this kind of dynamic GUI please tell me. thanks in advance.
One option would be to use the wxUpdateUIEvent in order to update the choice when the main frame is idle. If you're going to go that route, I would
m_choiceNeedsUpdate
or something like and an event handler void OnUpdateUI(wxUpdateUIEvent& event)
(or what ever you want to call it) to the class of your application's form.this->Bind(wxEVT_UPDATE_UI,&MyFrame::OnUpdateUI,this);
When you do something that requires the choice to be updated, you can arrange for it to be updated with calls like these:
m_choiceNeedsUpdate=true;
this->UpdateWindowUI();
The body of the event handler where the choice control is updated might look something like this
void MyFrame::OnUpdateUI(wxUpdateUIEvent& event)
{
if (m_choiceNeedsUpdate)
{
//Update the choice control here (probably using the Clear/Delete methods)
m_choiceNeedsUpdate=false;
}
}
The advantage of going this route is that all of the logic concerning updating the UI can be placed in one method/event handler. This is especially nice if you have several controls that might need to be dynamically updated.
The downside is that there will be a lot of calls to that event handler when your frame is running which can potentially effect performance. That's why I guarded the logic for changing the choice control with the m_choiceNeedsUpdate bool variable in the example above.