【发布时间】:2016-05-20 16:37:59
【问题描述】:
首先让我说我这几天一直在寻找解决方案......
我正在尝试获取 ListBox 的选定项。这是我的代码:
CListBox * pList1 = (CListBox *)GetDlgItem(IDC_LIST1);
CString ItemSelected;
// Get the name of the item selected in the Sample Tables list box
// and store it in the CString variable declared above
pList1->GetText(pList1->GetCurSel(), ItemSelected);
MessageBox(ItemSelected, "TEST", MB_OK);
现在,当我尝试此操作时,我收到一条错误消息,提示“参数不正确”
【问题讨论】:
-
pList1->GetCurSel()不能保证成功(列表框可以完全没有选择)。在这种情况下,它返回LB_ERR。这不是传递给CListBox::GetText的有效索引。 -
但是有一个选择......还有其他方法吗?
-
先尝试完整编译。然后尝试调试它。此外,为了进行测试,请将
GetCurSel值分配给一个变量,以便您确认。最后,您为什么不直接将CListBox映射到CString变量而只是调用UpdateData(TRUE)并使用变量值? -
你的ClistBox是单选还是多选?