In order to have a table like:
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:
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?
Let us look into a simple example by creating a new MFC dialog based application. Step 1 − Delete the TODO line and drag one List Control. Step 2 − In the Properties Window, you will see the different options in View dropdown list. Step 3 − Select the Report from the View field.
Step 1 − To create a dialog box, right-click on the Resource Files folder in solution explorer and select Add → Resource. Step 2 − In the Add Resource dialog box, select Dialog and click New. Step 3 − A dialog box requires some preparation before actually programmatically creating it.
First, make sure you chose the Report
option of the View
property of the List Control in the Resource Editor. I suspect that you are using the default Icon
view, which is not what you want.
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 ....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With