[英]How to add items to a List Control in an MFC dialog
In order to have a table like: 为了有一个像这样的表:
in my MFC dialog, I have added a List Control
to it. 在我的MFC对话框中,我向其中添加了一个List Control
。 And then with Add Variable
wizard, I have created this variable for the control: 然后使用“ Add Variable
向导,为控件创建了此变量:
public:
CListCtrl m_lstIDC_LIST1Control;
and then in the OnInitDialog
function 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: 我得到的结果是:
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? 为了创建一个如第一张图所示的表格,我应该怎么做?
First, make sure you chose the Report
option of the View
property of the List Control in the Resource Editor. 首先,请确保您在资源编辑器中选择了列表控件的“ View
属性的“ Report
选项。 I suspect that you are using the default Icon
view, which is not what you want. 我怀疑您使用的不是默认的“ 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 .... 等等 ....
另外,还要确保您拥有正确的控件类型……您想要(至少在Visual Studio 2008的资源编辑器中)在工具箱(而不是列表框)中称为列表控件的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.