5

In order to have a table like:

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

public:
CListCtrl m_lstIDC_LIST1Control;  

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

// 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?

4

2 回答 2

24

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

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

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

最后,您可以简单地填充列表项,如下所示:

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

等等 ....

于 2013-09-14T13:56:46.717 回答
2

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

于 2015-02-28T09:40:59.200 回答