首先让我说几天来我一直在寻找解决方案.
我正在尝试为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);现在,当我尝试这样做时,我会收到一条错误消息,上面写着“参数是incorect”
发布于 2016-05-20 17:54:06
除了错误处理之外,您的代码看起来还可以。此外,MessageBox参数看起来不正确。第一个参数应该是HWND类型。我相信这是你们问题的根源。改用MFC标准AfxMessageBox:
CListBox * pList1 = (CListBox *)GetDlgItem(IDC_LIST1);
int nSel = pList1->GetCurSel();
if (nSel != LB_ERR)
{
CString ItemSelected;
pList1->GetText(nSel, ItemSelected);
AfxMessageBox(ItemSelected);
}发布于 2016-05-20 18:40:45
如果CListBox处于单一选择模式,则CListBox::GetCurSel将返回所选索引。
如果CListBox处于多选择模式,则应使用CListBox::GetSelItems返回索引列表。
您不能混合“n”来匹配这些函数。
并且始终检查返回代码(就像其他人已经写的那样)。
发布于 2017-08-07 14:02:19
如果您已经有一个数据成员MyList(of classCListBox):
int nSel = MyList.GetCurSel();
CString ItemSelected;
if (nSel != LB_ERR)
{
MyList.GetText(nSel, ItemSelected);
}https://stackoverflow.com/questions/37351716
复制相似问题