468,117 Members | 1,838 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,117 developers. It's quick & easy.

ListViewItems from array


lvProjects.Clear()
lvProjects.View = View.Details
lvProjects.FullRowSelect = True
lvProjects.Columns.Add("Id", 50, HorizontalAlignment.Left)
lvProjects.Columns.Add("Name", -1, HorizontalAlignment.Left)
lvProjects.Columns.Add("Last Used", 100, HorizontalAlignment.Center)
lvProjects.Columns.Add("Status", 100, HorizontalAlignment.Center)
Dim Item1 As New ListViewItem("001", 0)
Item1.SubItems.Add("ABC Soliciters")
Item1.SubItems.Add("12/01/2003")
Item1.SubItems.Add("Idle")
Dim Item2 As New ListViewItem("002", 0)
Item2.SubItems.Add("ABC Soliciters")
Item2.SubItems.Add("12/01/2002")
Item2.SubItems.Add("Idle")
Dim al As New ArrayList()
al.Add(Item1)
al.Add(Item2)

Question: How do I assign al to in the following line?
lvProjects.Items.AddRange(New ListViewItem() {how do i assign al here})

This works below:
lvProjects.Items.AddRange(New ListViewItem() {item1,item2})

Thanks 4 any help!!
Nov 20 '05 #1
8 8443
Hi Chad,

Have you tried simply:
lvProjects.Items.AddRange (al)
or
lvProjects.Items.AddRange (al.ToArray)

Regards,
Fergus
Nov 20 '05 #2
* "Fergus Cooney" <fi****@post.com> scripsit:
Have you tried simply:
lvProjects.Items.AddRange (al)
or
lvProjects.Items.AddRange (al.ToArray)


Have you tried that?

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #3
Why do you ask?
Nov 20 '05 #4
* "Fergus Cooney" <fi****@post.com> scripsit:
Why do you ask?


Try it and you will see it...

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #5
Hi Chad,

The second option in my earlier post is the one you want but it needed a
bit of casting:

With Option Strict Off:
lvProjects.Items.AddRange (al.ToArray (GetType (ListViewItem)))

With Option Strict On:
Dim ao As Array = al.ToArray (GetType (ListViewItem))
lvProjects.Items.AddRange (DirectCast (ao, ListViewItem()))

But it might be easier to assign the ListViewItems to an array of a
suitable type in the first place.
Dim alv As ListViewItem (1)
alvItems(0) = Item1
alvItems(1) = Item2
lvProjects.Items.AddRange (alvItems)
Regards,
Fergus
Nov 20 '05 #6
Thanks, that is what I ended up doing (using an array). Sometimes its nice
to use the arraylist not to worry about array length. Thanks again!!!

One other thing .. If you populate the listview with column size property
to -1 for auto resize the first time it is populated using the method we
have discussed it works fine. But each time after that it sets the column
length to 0. Any ideas why?

"Fergus Cooney" <fi****@post.com> wrote in message
news:OX**************@TK2MSFTNGP10.phx.gbl...
Hi Chad,

The second option in my earlier post is the one you want but it needed a bit of casting:

With Option Strict Off:
lvProjects.Items.AddRange (al.ToArray (GetType (ListViewItem)))

With Option Strict On:
Dim ao As Array = al.ToArray (GetType (ListViewItem))
lvProjects.Items.AddRange (DirectCast (ao, ListViewItem()))

But it might be easier to assign the ListViewItems to an array of a
suitable type in the first place.
Dim alv As ListViewItem (1)
alvItems(0) = Item1
alvItems(1) = Item2
lvProjects.Items.AddRange (alvItems)
Regards,
Fergus

Nov 20 '05 #7
* "Chad Miller" <ch**@predictiveconcepts.com> scripsit:
lvProjects.Clear()
lvProjects.View = View.Details
lvProjects.FullRowSelect = True
lvProjects.Columns.Add("Id", 50, HorizontalAlignment.Left)
lvProjects.Columns.Add("Name", -1, HorizontalAlignment.Left)
lvProjects.Columns.Add("Last Used", 100, HorizontalAlignment.Center)
lvProjects.Columns.Add("Status", 100, HorizontalAlignment.Center)
Dim Item1 As New ListViewItem("001", 0)
Item1.SubItems.Add("ABC Soliciters")
Item1.SubItems.Add("12/01/2003")
Item1.SubItems.Add("Idle")
Dim Item2 As New ListViewItem("002", 0)
Item2.SubItems.Add("ABC Soliciters")
Item2.SubItems.Add("12/01/2002")
Item2.SubItems.Add("Idle")
Dim al As New ArrayList()
al.Add(Item1)
al.Add(Item2)

Question: How do I assign al to in the following line?
lvProjects.Items.AddRange(New ListViewItem() {how do i assign al here})

This works below:
lvProjects.Items.AddRange(New ListViewItem() {item1,item2})


\\\
Dim al As New ArrayList
al.Add(New ListViewItem("Hello"))
al.Add(New ListViewItem("World"))
Me.ListView1.Items.AddRange(DirectCast(al.ToArray( GetType(ListViewItem)), ListViewItem()))
///

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #8
Hi Chad,

That Column.Width = -1 is a bit misleading in the sense that if you set
the Width to -1 when you create it,
lvProjects.Columns.Add("Name", -1, HorizontalAlignment.Left)
it sets the width to zero because there are no items, therefore the
greatest width = 0.

It's really for use when you've got your items in place. So if you add

lvProjects.Columns(1).Width = -1
after the items have been added, it will work ok.

And you'll have to keep setting it when you add items or remove items
because it's not a dynamic setting. It would be better, in fact, if they had
a method Column.SetWidthToFit or something. This would make it clear that
you have to keep doing it. For .NET, using Width = -1 seems a pretty kludgy
way of doing it.

Regards,
Fergus
Nov 20 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brian | last post: by
2 posts views Thread by Stormkid | last post: by
8 posts views Thread by vcardillo | last post: by
12 posts views Thread by Sam Collett | last post: by
8 posts views Thread by Mike S. Nowostawsky | last post: by
1 post views Thread by =?Utf-8?B?VmljdG9y?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.