wxWidgets 与附加到 wxChoice 列表相反

wxWidgets oposite of appending to a wxChoice list

我正在为一个大学项目构建一个基本的 GUI,我正在使用 wxwidgets 和代码块,因为 c++ 是我的舒适编程语言。但是,我想修改 wxChoice 上的选项,因为这些选项并不意味着总是相同的,这意味着如果我将某些内容附加到选择列表,然后我想将列表更新为一个完全不同的列表,首先我必须从选择列表中删除旧项目,然后添加新项目,但我找不到该怎么做。这是我的代码:

//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]!='[=10=]'){
        if(str[i]==','){
            Choice2->Append(_(str2));
            i++;
            str2="";
        }else{
            str2+=str[i];
            i++;
        }
    }
}

此外,如果有更好的方法来实现这种动态 GUI,请告诉我。提前致谢。

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

您可以使用 wxChoice::Clear() 方法从选择控件中删除所有条目,您可以使用 wxChoice::Delete(unsigned int n) 方法从控件中删除特定条目.

它们列在 "Public Member Functions inherited from wxItemContainer" 部分的 documentation page 中。

Also if there's a better way to do this kind of dynamic GUI please tell me. thanks in advance.

一种选择是使用 wxUpdateUIEvent 以便在主机空闲时更新选择。如果你要走那条路,我会

  1. 将一个名为 m_choiceNeedsUpdate 或类似名称的 bool 成员和一个事件处理程序 void OnUpdateUI(wxUpdateUIEvent& event)(或任何您想调用的名称)添加到应用程序表单的 class。
  2. 使用类似 this->Bind(wxEVT_UPDATE_UI,&MyFrame::OnUpdateUI,this);
  3. 的调用在框架构造函数中绑定事件处理程序
  4. 当你做一些需要更新选择的事情时,你可以安排它通过这样的调用来更新:

    m_choiceNeedsUpdate=true;
    this->UpdateWindowUI();
    
  5. 更新选择控件的事件处理程序的主体可能如下所示

    void MyFrame::OnUpdateUI(wxUpdateUIEvent& event)
    {
        if (m_choiceNeedsUpdate)
        {
            //Update the choice control here (probably using the Clear/Delete methods)
            m_choiceNeedsUpdate=false;
        }
    }
    

走这条路的好处是所有关于更新 UI 的逻辑都可以放在一个 method/event 处理程序中。如果您有多个可能需要动态更新的控件,这尤其有用。

缺点是当您的帧为 运行 时,将有大量对该事件处理程序的调用,这可能会影响性能。这就是为什么我在上面的示例中使用 m_choiceNeedsUpdate bool 变量来保护更改选择控件的逻辑。