Hi all


I am trying to create ClistCtrl with check boxes and different columns in it.

BUT

ListCtrl m_listBox;

int

i;
CString strTemp;

m_listBox.Create(WS_CHILD|WS_VISIBLE|LBS_STANDARD|WS_HSCROLL, CRect(10,10,200,200),this, 111);


m_listBox.InsertColumn(0, _T("Column 1") , LVCFMT_LEFT, 50, 0);
m_listBox.InsertColumn(1, _T("Column 2") , LVCFMT_CENTER, 100, 1);
m_listBox.InsertColumn(2, _T("Column 3") , LVCFMT_RIGHT, 100, 2);

m_listBox.SetExtendedStyle(m_listBox.GetExtendedStyle()|LVS_EX_CHECKBOXES | LVS_REPORT );



for(i = 0; i < 10; i ++)
{

strTemp.Format(_T("item"), i + 1);
m_listBox.InsertItem(i, strTemp, 0);

strTemp.Format(_T("Subitem"), i + 1);
m_listBox.SetItemText(i, 1, strTemp);

strTemp.Format(_T("SubSubitem"), i + 1);
m_listBox.SetItemText(i, 2, strTemp);

}

after using this code i can see repetition of "item" with check boxes in single column.
please tell me what is wrong OR just write few lines of code for me.

regards

Abid

Dani AI

Generated

Two separate issues are visible in the original post.

First, the repeated "item" text is just the Format() misuse that already flagged — you need a format specifier (for example "%d") to insert the loop index into the string. Second, the reason you only see a single column with no headers is that the control is being created without the list‑view "report" style (or that LVS_REPORT was accidentally mixed into the extended styles). A plain listbox or a control created without LVS_REPORT will not show the header or multiple columns.

A minimal set of fixes (create the control as a report view, set only list‑view extended styles, and use a proper Format string):

m_listBox.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT | WS_BORDER | WS_HSCROLL | WS_VSCROLL,
                 CRect(10,10,200,200), this, 111);

m_listBox.SetExtendedStyle(m_listBox.GetExtendedStyle() |
                          LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);

strTemp.Format(_T("Item %d"), i + 1);

Troubleshooting checklist:

  • Make sure the member is a CListCtrl (not a CListBox) and that you create it with LVS_REPORT (or set the resource control to "Report" in the dialog editor).
  • Do not OR LVS_REPORT into SetExtendedStyle — LVS_REPORT is a window style, LVSEX* constants are extended styles.
  • Call InsertColumn to add headers (widths > 0), then InsertItem / SetItemText for rows. Columns can be inserted before or after items, but the control must be in report mode to show the header.
  • If you change styles, recreate the control (styles are fixed at creation).

Following those three changes — correct Format string, LVS_REPORT at creation, and proper extended styles — will produce the multi‑column list with checkboxes and visible headers.

Recommended Answers

All 4 Replies

>>strTemp.Format(_T("item"), i + 1);

Format() function is very similar to sprintf() -- you need to add %d if you want the string to contain the value of i+1. strTemp.Format(_T("item %d"), i + 1);

i have tried it but does not work. i am using visual studio 2005 :(

what do you mean "it doesn't work".

i mean it does not work. i want 3 columns in my Clistctrl but i can see only on column. no heading of columns but only list of
item
item
item
item
item
item
item
item

hope you understand

:(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.