-
Oct 11th, 2018, 08:11 AM
#1
Thread Starter
Lively Member
Setting columns in a list box
I have a list box and I want it to have columns. In vb6, it was easy. All you had to do is set the columns properties to however many columns you wanted but I can't seem to find it on VB.Net
-
Oct 11th, 2018, 08:26 AM
#2
Re: Setting columns in a list box
Hi,
try a Listview
just add a Listview and put this in Form Load
Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
With ListView1
.View = View.Details
.FullRowSelect = True
.GridLines = True
.HideSelection = False
.Columns.Add("Column 1")
.Columns.Add("Column 2")
.Columns.Add("Column 3")
.Columns.Add("Numbers")
.Columns(3).TextAlign = HorizontalAlignment.Right
.Columns.Add("Money")
.Columns(4).TextAlign = HorizontalAlignment.Right
.Columns.Add("Date")
.Columns.Add("Coulmn 6")
Dim R As New Random()
For i As Integer = 0 To 50
Dim Li As New ListViewItem
Li.Text = "Item " & i.ToString & ".1"
Li.SubItems.Add("Item " & i.ToString & ".2")
Li.SubItems.Add("Item " & i.ToString & ".3")
Dim Zahl As Integer = R.Next(100, 10000000)
Li.SubItems.Add(Zahl.ToString("#,###"))
Dim Geld As Decimal = R.Next(1, 100000000)
Li.SubItems.Add(Geld.ToString("#,##0.00"))
Dim Datum As Date = Date.Now.AddSeconds(Convert.ToDouble(R.Next(1, 1000000)))
Li.SubItems.Add(Datum.ToString("dd.MM.yyyy HH:mm:ss"))
Li.SubItems.Add("Item " & i.ToString & ".7")
.Items.Add(Li)
Next
.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent)
End With
End Sub
End Class
regards
Chris
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Oct 11th, 2018, 08:26 AM
#3
Re: Setting columns in a list box
A ListBox is not designed to have columns because it should be used to display a collection of values in a singular fashion from top-to-bottom. You can achieve the column view somewhat by setting the font to a monospaced family (e.g. Consolas, Courier New, Lucida Sans Typewriter, etc.) and then append spaces to the shorter lengthed items.
Here is a quick example of setting it up using that hack:
Code:
With ListBox1
.Font = New Font("Consolas", 10, FontStyle.Regular, GraphicsUnit.Pixel)
.Items.AddRange({
"Cell - 1 | Cell - 2",
"Cell3 | Cell4",
"Cell-5 | Cell-6"})
End With
But ideally you should use a control that is designed to handle data like a DataGridView.
-
Oct 11th, 2018, 11:30 AM
#4
Re: Setting columns in a list box
If you have data that makes sense in multiple columns, then either a ListView in detail mode or a DGV are better ways to display the data. The DGV is similar to the MSFlexGrid that was found in VB6, but the MSFlexGrid was deficient in several, annoying, ways, which have been fixed in the DGV. However, if you use a DGV, it makes most sense to have your data in a datatable. Datatables are pretty much what they sound like: Tables of data. They have lots of methods that make it convenient to search them (though you have at least four major ways of searching them, with some variations of those main ways). Displaying a datatable in a DGV is also particularly easy, as is editing the data by that means.
In contrast, I find ListViews a bit more awkward to work with. ListViews makes most sense to me when I either want to associate an icon or other image with a record, or if the data doesn't conceptually fit very well into a datatable. When I think of a multi-column listbox, I expect the data behind that to be tabular data, in which case I'd want it in a datatable just for the convenience of searching, sorting, filtering, storage, retrieval, and so on, and once the data is in a datatable, then a DGV is by far the easiest way to display the data, as you just set the .DataSource property of the DGV to your datatable...and the rest is taken care of.
My usual boring signature: Nothing
-
Oct 11th, 2018, 11:01 PM
#5
Re: Setting columns in a list box
Originally Posted by codemonster
In vb6, it was easy. All you had to do is set the columns properties to however many columns you wanted
I'm not sure that that's true. I've never used VB6 so maybe I'm mistaken but I just did a quick searched and what I found indicates that a ListBox doesn't have columns in VB6. Are you sure that you're not thinking of a ListView, which is available and does support columns in VB.NET too? That said, a Listview should generally not be used for tabular data. If you're not using at least one view besides Details or using groups in a VB.NET ListView then you should almost certainly be using a DataGridView instead.
-
Oct 12th, 2018, 07:20 AM
#6
Re: Setting columns in a list box
Originally Posted by Shaggy Hiker
The DGV is similar to the MSFlexGrid that was found in VB6, but the MSFlexGrid was deficient in several, annoying, ways, which have been fixed in the DGV.
I can't agree with that, sadly the Datagridview can't create the headers like MSFlexgrid
Image:
or if you know a way it would be a great help.
regards
Chris
Last edited by ChrisE; Oct 12th, 2018 at 07:25 AM.
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Oct 12th, 2018, 08:06 AM
#7
Re: Setting columns in a list box
Originally Posted by ChrisE
I can't agree with that, sadly the Datagridview can't create the headers like MSFlexgrid
Image:
or if you know a way it would be a great help.
regards
Chris
Never tried it myself but this looks like what you need:
https://social.msdn.microsoft.com/Fo...a-datagridview
That's the thing with the DGV: you can do all sorts of customisation for the stuff that isn't supported out of the box. If you wanted to, you could inherit the class and then add appropriate functionality to make multiple rows and arbitrarily merged column headers reusable.
-
Oct 13th, 2018, 01:43 AM
#8
Re: Setting columns in a list box
Originally Posted by jmcilhinney
Never tried it myself but this looks like what you need:
https://social.msdn.microsoft.com/Fo...a-datagridview
That's the thing with the DGV: you can do all sorts of customisation for the stuff that isn't supported out of the box. If you wanted to, you could inherit the class and then add appropriate functionality to make multiple rows and arbitrarily merged column headers reusable.
H jmc,
yes I did see that thread some time ago and tried it. The Header -flickers-when scrolling quit bad
regards
Chris
Last edited by ChrisE; Oct 13th, 2018 at 01:50 AM.
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|