AddRange method for CheckedListBox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I want to add multiple items (about 20) to a checked list box control at run time, but do not know how to use the AddRange method of the Items collection. Currently, I am using the Add method, which works fine, but since I'm dealing with 20 items, this means I have 20 lines of code because the Add method is used once for every item

Could somebody please clarify the syntax of the AddRange method? I know you're supposed to use braces "{ }", but I'm just not sure where they go, and IntelliSense has got me all confused

Thanks and Best Regards
David Morris
 
Hi David,

You need to pass either an ObjectCollection object or an array to the
AddRange method; these must be defined beforehand. For example:

Dim myItems As New ListBox.ObjectCollection(CheckedListBox1)
myItems.Add("foo")
myItems.Add("bar")
myItems.Add("foobar")

CheckedListBox1.Items.AddRange(myItems)

If you are only loading the ListBox once, this doesn't save you any work
over using the Add method, but if you have a common set of ListBox items
that are used in multiple places, using AddRange with an ObjectCollection or
array means you only have to list the items once.

Hope this helps,
Steve Hoag
Visual Basic .NET team
 
* =?Utf-8?B?RGF2aWQgTW9ycmlz?= said:
Could somebody please clarify the syntax of the AddRange method? I
know you're supposed to use braces "{ }", but I'm just not sure where
they go, and IntelliSense has got me all confused.

\\\
Me.CheckedListBox1.Items.AddRange(New Object() {"Hello", "World", "Foo", 1, 2, "Goo", 1.2})
///

Replace the 'Object' with the type of items if you add only items of the
same type.
 
Back
Top