如何清除wxListBox中的所有选择(使用wxLB_MULTIPLE)

How to clear all selections in a wxListBox (with wxLB_MULTIPLE)?

本文关键字:使用 有选择 MULTIPLE wxLB 何清除 清除 wxListBox      更新时间:2023-10-16

如何清除样式为wxLB_MULTIPLE的wxListBox中的所有选择

wxControlWithItems::SetSelection()和wxControl WIthItems::SetStringSelection()都不起作用。他们只选择通过的项目,但没有像文件中所说的那样取消选择任何其他项目:

请注意,这不会导致发出任何命令事件,也不会它是否取消选择控件中支持的任何其他项多重选择。

我会使用wxListBox::GetSelections然后是wxListBox::取消选择,因为没有取消选择所有方法。

独立于平台的方法

正如StevenL所提到的,您必须在一个循环中使用wxListBox::GetSelections()wxListBox::Deselect()

wxArrayInt selections;
int count = m_listBox->GetSelections(selections);
for ( int i=0; i<count; i++ )
{
  m_listBox->Deselect(selections[i]);
}

ListBox_SetSet宏(适用于Windows系统)

您可以使用Windows API中的宏ListBox_SetSet。

对于这个函数,您必须包括Windowsx.hwx/msw/winundef.h,当然:

#include <Windowsx.h>
#include <wx/msw/winundef.h>
// ...
HWND hListBox = (HWND)m_listBox->GetHandle();
ListBox_SetSel(hListBox, false, -1);

m_listBox->清除();这种方法适用于我