Hi guys I am creating a basic application and have set up a database in which i can pull data but im struggling to figure out how to split a listbox into 2 columns, any ideas would be much appreciated

Thanks in advance


If you want to display two columns of data and assuming this is a Windows Forms application then maybe using the ListView control would be a better approach.


yeah it is a windows form, ok i will have a look and a play, i know list boxes can be split i have seen it before just dont have a clue how to do it

Why not use a DataGridView control?
Or else use the multicolumn property of the listbox.

What do you mean by split? Do you want data in two columns like a table for example:

Column1 Column2
Data Data
Data Data

If so, add a ListView to your form then in the load event of your form add the following code (and read the comments to see what is happening):

// Set the ListView to Details view (the listview supports large icons, small icons, list)
// in exactly the same way as Windows Explorer does. In the case of splitting data we want
// a Details view so that we can have columns.
listView1.View = View.Details;

// Add 2 columns to the list view. This can be done in the properties of the list view
// look at the Columns property, or code as we are doing here.
listView1.Columns.Add("Column 1");
listView1.Columns.Add("Column 2");

// Now we will add some data. I'm just using a loop to show how data will be represented
// Data + i is added as the main item - the first column - and sub item + i is added as 
// an additional item.

// There are other methods for adding items but this is just an example. 
for (int i = 0; i < 10; i++)
    listView1.Items.Add("Data " + i.ToString()).SubItems.Add("Sub item " + i.ToString());

Is this what you mean?

commented: Clear explanation +15

Or else use the multicolumn property of the listbox.

I agree with the DataGridView as an alternative but I'm not sure about the MultiColumn property of a list box as this is used to negate the need to scroll vertically. You also have no control over how many columns are created as this is totally dependant on the data added to the list box.

Also, there are no column headers, so unless you know the actual width that you will need, some data may be truncated (or hidden) and the user will not be able to resize the columns.

As it is said by the previous writers, you may use a ListView

Ok thanks very much guys yeah the list view seems the best option for what I plan on doing I will have a play tomorrow and see what I can come up with

Thanks all yet again

Be a part of the DaniWeb community

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