C++ 如何在 MFC 对话框中向列表控件添加项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18802291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:13:29  来源:igfitidea点击:

How to add items to a List Control in an MFC dialog

c++visual-studio-2010listviewmfc

提问by Sepideh Abadpour

In order to have a table like:

为了有一张像:

enter image description here
in my MFC dialog, I have added a List Controlto it. And then with Add Variablewizard, I have created this variable for the control:

在此处输入图片说明
在我的 MFC 对话框中,我添加了一个List Control。然后使用Add Variable向导,我为控件创建了这个变量:

public:
CListCtrl m_lstIDC_LIST1Control;  

and then in the OnInitDialogfunction of my dialog, I have added these lines of code:

然后在OnInitDialog我的对话框的功能中,我添加了以下代码行:

// TODO: Add extra initialization here
m_lstIDC_LIST1Control.SetExtendedStyle(LVS_EX_FULLROWSELECT);
m_lstIDC_LIST1Control.SetExtendedStyle(LVS_EX_GRIDLINES);
//m_lstIDC_LIST1Control.SetExtendedStyle( LVS_SHOWSELALWAYS);
LVITEM lvItem;

lvItem.mask = LVIF_TEXT;
lvItem.iItem = 0;
lvItem.iSubItem = 0;
char* text = "Sandra C. Anschwitz";
wchar_t wtext[50];
mbstowcs(wtext, text, strlen(text)+1);
LPWSTR ptr = wtext;
lvItem.pszText = ptr;
m_lstIDC_LIST1Control.InsertItem(&lvItem);
UpdateData(false);  

the result that I get is:

我得到的结果是:

enter image description here
and if I uncomment the line:

在此处输入图片说明
如果我取消注释该行:

//m_lstIDC_LIST1Control.SetExtendedStyle( LVS_SHOWSELALWAYS);  

the horizontal grids will not be shown either!
So what's the problem?
Why the item that I have added is not shown? what should I do in order to create a table like the one shown in the first picture?

水平网格也不会显示!
所以有什么问题?
为什么我添加的项目没有显示?我应该怎么做才能创建像第一张图片中显示的表格?

回答by Roger Rowland

First, make sure you chose the Reportoption of the Viewproperty of the List Control in the Resource Editor. I suspect that you are using the default Iconview, which is not what you want.

首先,确保您在资源编辑器中选择了列表控件ReportView属性选项。我怀疑您使用的是默认Icon视图,这不是您想要的。

Then, you need to add the required columns:

然后,您需要添加所需的列:

m_lstIDC_LIST1Control.InsertColumn(0, _T("Full Name"), LVCFMT_LEFT, 90);
m_lstIDC_LIST1Control.InsertColumn(1, _T("Profession"), LVCFMT_LEFT, 90);
m_lstIDC_LIST1Control.InsertColumn(2, _T("Fav Sport"), LVCFMT_LEFT, 90);
m_lstIDC_LIST1Control.InsertColumn(3, _T("Hobby"), LVCFMT_LEFT, 90);

Finally, you can populate your list items simply as follows:

最后,您可以简单地按如下方式填充列表项:

int nIndex = m_lstIDC_LIST1Control.InsertItem(0, _T("Sandra C. Anschwitz"));
m_lstIDC_LIST1Control.SetItemText(nIndex, 1, _T("Singer"));
m_lstIDC_LIST1Control.SetItemText(nIndex, 2, _T("Handball"));
m_lstIDC_LIST1Control.SetItemText(nIndex, 3, _T("Beach"));

nIndex = m_lstIDC_LIST1Control.InsertItem(1, _T("Roger A. Miller"));
m_lstIDC_LIST1Control.SetItemText(nIndex, 1, _T("Footballer"));
m_lstIDC_LIST1Control.SetItemText(nIndex, 2, _T("Tennis"));
m_lstIDC_LIST1Control.SetItemText(nIndex, 3, _T("Teaching"));

And so on ....

等等 ....

回答by Swiss Frank

Also make sure you've got the right kind of control... you want what (at least on Visual Studio 2008's Resource Editor) is called a List Control in the toolbox, not the List Box.

还要确保您拥有正确的控件类型……您想要(至少在 Visual Studio 2008 的资源编辑器中)在工具箱中称为列表控件,而不是列表框。