简体   繁体   English

MFC C ++ CListBox获取所选项目

[英]MFC C++ CListBox get selected item

First let me say that I've been searching for a solution for couple of days now... 首先让我说,我一直在寻找解决方案已有几天了...

I'm trying to get selected item for ListBox. 我正在尝试获取ListBox的选定项目。 This is my code: 这是我的代码:

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);

Now when i try this i get an error message saying "The Parameter is incorect" 现在,当我尝试此操作时,我收到一条错误消息,提示“参数不正确”

Your code looks OK except error handling. 您的代码看起来不错,除了错误处理。 Also MessageBox parameters look incorrect. 此外, MessageBox参数看起来不正确。 The first parameter should be of type HWND . 第一个参数应为HWND类型。 I believe that this is the root cause of your problems. 我相信这是您遇到问题的根本原因。 Use MFC standard AfxMessageBox instead: 改用MFC标准AfxMessageBox

CListBox * pList1 = (CListBox *)GetDlgItem(IDC_LIST1);

int nSel = pList1->GetCurSel();
if (nSel != LB_ERR)
{
    CString ItemSelected; 
    pList1->GetText(nSel, ItemSelected);
    AfxMessageBox(ItemSelected);
}

If the CListBox is in single selection mode, the CListBox::GetCurSel will return the selected index. 如果CListBox处于单选模式,则CListBox :: GetCurSel将返回所选索引。

If the CListBox is in multi-selection mode, you should use CListBox::GetSelItems which will return a list of indices. 如果CListBox处于多选模式,则应使用CListBox :: GetSelItems,它将返回索引列表。

You cannot mix'n'match the functions. 您不能混合使用功能。

And always check return codes (as others already wrote). 并始终检查返回码(就像其他人已经写过的一样)。

If You already have a data member MyList(of classCListBox) : 如果您已经有一个数据成员MyList(属于classCListBox):

int nSel = MyList.GetCurSel();
    CString ItemSelected;
    if (nSel != LB_ERR)
    {
        MyList.GetText(nSel, ItemSelected);
    }

CWnd class has a MessageBox function which does not need a HWND parameter. CWnd类具有一个MessageBox函数,该函数不需要HWND参数。 But yes, AfxMessageBox is a little bit more easier to use and can be called anywhere in the MFC code without having a CWnd-derived object. 但是,是的, AfxMessageBox更加易于使用,并且可以在MFC代码中的任何位置调用而无需CWnd派生的对象。 And a beside note: if call a WinAPI function inside MFC code (not needed here, but possible in other cases) it's good to prepend it with scope resolution operator in order to avoid any confusion, mistake and/or name conflict (eg ::MessageBox ...). 还有一个注意事项:如果在MFC代码中调用WinAPI函数(此处不需要,但是在其他情况下可能是),最好在范围解析运算符前添加它,以避免任何混淆,错误和/或名称冲突(例如:: MessageBox ...)。

One possible cause for "invalid parameter" error in OP code is that it uses an ANSI string literal ("TEST") in a UNICODE build configuration. OP代码中“无效参数”错误的一种可能原因是,它在UNICODE构建配置中使用了ANSI字符串文字(“ TEST”)。 This case, must use an UNICODE string literal (L"TEST") or a little bit better, use _T macro (_T("TEST")) that makes it possible to build in both ANSI and UNICODE configurations. 在这种情况下,必须使用UNICODE字符串文字(L“ TEST”)或更好一点,使用_T宏(_T(“ TEST”)),以便可以在ANSI和UNICODE配置中进行构建。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM