gpt4 book ai didi

c++ - MFC C++ CListBox 获取选中项

转载 作者:搜寻专家 更新时间:2023-10-31 00:10:39 27 4
gpt4 key购买 nike

首先让我说,我这几天一直在寻找解决方案...

我正在尝试为 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);

现在,当我尝试这个时,我收到一条错误消息,提示“参数不正确”

最佳答案

除了错误处理之外,您的代码看起来还不错。此外,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);
}

关于c++ - MFC C++ CListBox 获取选中项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37351716/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com