AddRange method for CheckedListBox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Morris

    AddRange method for CheckedListBox

    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
  • Steve Hoag [MS]

    #2
    Re: AddRange method for CheckedListBox

    Hi David,

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

    Dim myItems As New ListBox.ObjectC ollection(Check edListBox1)
    myItems.Add("fo o")
    myItems.Add("ba r")
    myItems.Add("fo obar")

    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 ObjectCollectio n or
    array means you only have to list the items once.

    Hope this helps,
    Steve Hoag
    Visual Basic .NET team

    --
    This posting is provided "AS IS" with no warranties, and confers no rights.
    "David Morris" <anonymous@disc ussions.microso ft.com> wrote in message
    news:A2052B7B-0E33-419C-A595-4E73469C2299@mi crosoft.com...[color=blue]
    > 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[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: AddRange method for CheckedListBox

      * =?Utf-8?B?RGF2aWQgTW9 ycmlz?= <anonymous@disc ussions.microso ft.com> scripsit:[color=blue]
      > 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.[/color]

      \\\
      Me.CheckedListB ox1.Items.AddRa nge(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.

      --
      Herfried K. Wagner [MVP]
      <URL:http://dotnet.mvps.org/>

      Comment

      Working...