windows 如何创建多列列表框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2166473/
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
How to create a multicolumn Listbox?
提问by kampi
I'm working on a program, which should list all files and it's size(for now...). I created a simple application, which should write the data to a listbox. I'm trying to write the data in two columns(the first should be the name, and next to it, in an other column, it's size), but i can't figure out, how should i do this. Can someone help me?
我正在开发一个程序,它应该列出所有文件和它的大小(现在......)。我创建了一个简单的应用程序,它应该将数据写入列表框。我试图在两列中写入数据(第一列应该是名称,在它旁边的另一列中,它是大小),但我不知道,我应该怎么做。有人能帮我吗?
Thanks in advance!
提前致谢!
kampi
坎皮
Update:
更新:
I try to use ListControl., but unfortunately i can't. I can succesfully compile my App, but i can only see, the empty rectangle. Does someone know what i am doing wrong?
我尝试使用 ListControl。,但不幸的是我不能。我可以成功编译我的应用程序,但我只能看到空矩形。有人知道我做错了什么吗?
BOOL CGetFileListDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
LVITEM lvItem;
LVCOLUMN lvColumn;
int nCol;
lvColumn.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
lvColumn.fmt = LVCFMT_CENTER;
lvColumn.cx = 10;
lvColumn.pszText = _T("Filename");
ListView_InsertColumn( m_List, 0, &lvColumn );
ListView_SetItemText( m_List, 0, 0, _T("TEST") );
return TRUE; // return TRUE unless you set the focus to a control
}
采纳答案by Sam
The list box control does support multiple columns, but it only supports a single series of entries; the multiple column support just makes the items continue onto the next columns so that vertical scrolling is not necessary.
列表框控件确实支持多列,但只支持单列条目;多列支持只是使项目继续到下一列,因此不需要垂直滚动。
As Kornel has suggested, a list view controlmay be more appropriate. After creating a list view control, use ListView_InsertColumnto create the columns. Then use ListView_SetItemTextto insert items.
正如 Kornel 所建议的,列表视图控件可能更合适。创建列表视图控件后,使用ListView_InsertColumn创建列。然后使用ListView_SetItemText插入项目。
EDIT:
My apoligies; you should use ListView_InsertItemto insert an item (a row) and then use ListView_SetItemTextto alter the subitems (columns). If the list view is still just a blank box without any headings, have you initialised common controls? This can be done using InitCommonControlsEx, specifying the ICC_LISTVIEW_CLASSES
constant. This should be done before creating the control.
编辑:我的 apoligies; 您应该使用ListView_InsertItem插入一个项目(一行),然后使用ListView_SetItemText更改子项目(列)。如果列表视图仍然只是一个没有任何标题的空白框,您是否初始化了常用控件?这可以使用InitCommonControlsEx来完成,指定ICC_LISTVIEW_CLASSES
常量。这应该在创建控件之前完成。
回答by Kornel Kisielewicz
Don't use a List Box, use a List Controlwith LVS_REPORT style.
不要使用列表框,使用带有 LVS_REPORT 样式的列表控件。
回答by eomeroff
Maybe to use DataGridView with object as data source.
也许将 DataGridView 与对象一起用作数据源。
回答by Santhosh Nagasanthosh
Three important parameters to be checked are
要检查的三个重要参数是
- List Box or the List Control (List Control is to be used)
- View parameter must be Report mode
- Owner Data must be set to False
The screenshot shows these
The programming flow to add data to list control are Change the list Control to extended list view(ListView_SetExtendedListViewStyle
), Create the layout(By adding columns), Add Item data (for each row needed) & add finally add sub-item to each column (for each item data added previously).
将数据添加到列表控件的编程流程是将列表控件更改为扩展列表视图(ListView_SetExtendedListViewStyle
)、创建布局(通过添加列)、添加项目数据(对于需要的每一行)以及最后将子项目添加到每列(对于之前添加的每个项目数据)。