c++ - 如何遍历 wxNotebook 的页面?

标签 c++ wxwidgets

void Mainframe::OnClearNotebook( wxCommandEvent& e )
{
    int end = m_notebook->GetPageCount();
    for ( int i = 0; i < end; i++ )
    {
        if ( m_notebook->GetPageText( i ) != "Server Log" )
        {
            m_notebook->DeletePage(i);
        }
    }
}

此代码在此表单中崩溃并显示消息“m_notebook->GetPageText() 索引超出范围”;

如何正确地遍历所有页面?

编辑,解决方案:

void Mainframe::OnClearNotebook( wxCommandEvent& e )
{   
    while ( m_notebook->GetPageCount() > 1 )
    {
        int end = m_notebook->GetPageCount() - 1;
        if ( m_notebook->GetPageText( end ) != "Server Log" )
        {
            m_notebook->DeletePage(end);
        }
        else
        {
            break;
        }
    }
}

最佳答案

如果您实际删除,GetPageCount() 将被更改,因此 end 将保留早期版本的值

例如。

你在 m_notebook->GetPageCount() 中有 100

所以结束 = 100

在迭代 10 个项目后,您将删除 5 个项目,所以现在列表将有 95 个项目,但您迭代到 100 个 - 这可能是问题

关于c++ - 如何遍历 wxNotebook 的页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19151343/

相关文章:

c++ - wxwidgets 是否在 Mac 中使用 rosetta?

C++ 默认参数值使用另一个参数的值

c++ - 添加十六进制值以获得结果十六进制

c++模板与成员函数指针的用法

c++ - C++中什么时候使用const_cast

c++ - 使用 wxWebViewHandler 注册一个协议(protocol)

c++ - wxWidgets 翻译不加载

python - 为什么我的水平调整器添加到垂直调整器后宽度会改变?

c++ - 为什么 cin 不能像 cin.get 那样识别回车键?

c++ - C++ 中的内存管理 : Creating objects in a loop without crashing the program