All these New Object()s are identical (and in fact, much longer!) How do I declare a single instance then reuse it in each ComboBox .Items.AddRange()? I am using Visual Studio 2008/Visual Basic.

verticalBarComboBox.Items.AddRange(New Object() {" single space", "- hyphen", " -  <single space>hyphen<single space>", "` open single quote", "=", "~", "!"})
 backSlashComboBox.Items.AddRange(New Object() {" single space", "- hyphen", " -  <single space>hyphen<single space>", "` open single quote", "=", "~", "!"})
 colonComboBox.Items.AddRange(New Object() {" single space", "- hyphen", " -  <single space>hyphen<single space>", "` open single quote", "=", "~", "!"})
 quoteComboBox.Items.AddRange(New Object() {" single space", "- hyphen", " -  <single space>hyphen<single space>", "` open single quote", "=", "~", "!"})

Thanks!

Dani AI

Generated

Short summary: the simplest fix is to keep the list of choices in one place and reuse it for every ComboBox instead of repeating the literal array in each call. That is the essence of ’s suggestion and the Form_Load approach shown by ; the example below is a slightly more robust pattern that avoids accidental shared-binding and keeps the source of truth in one spot for easier maintenance (thanks to for the clear problem statement).

A compact, maintainable pattern is to declare a single read-only collection at class/module scope and bind a fresh copy of it to each ComboBox. Binding a copy prevents later changes to one ComboBox from affecting the others:

Private Shared ReadOnly SymbolOptions As New List(Of String) From {
    "(space)",
    "hyphen",
    "hyphen with spaces",
    "open single quote",
    "=",
    "~",
    "!"
}

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim all = New ComboBox() {verticalBarComboBox, backSlashComboBox, colonComboBox, quoteComboBox}
    For Each cb As ComboBox In all
        cb.DataSource = New List(Of String)(SymbolOptions) ' assign a copy
    Next
End Sub

Notes and troubleshooting:

  • Setting DataSource binds the control; assigning a new list copy keeps each ComboBox independent. If the same live list is desired (so changes propagate to all), assign the same instance instead.
  • Items.AddRange (as suggested earlier) also works: it copies items into the control’s Items collection, so sharing a single array is safe — but DataSource is cleaner when the data might be replaced or comes from a single collection.
  • Ensure this runs after InitializeComponent() (e.g., Form_Load), and avoid mixing DataSource and direct Items manipulation on the same ComboBox.
  • For larger or localized sets, move the strings to resources or settings for easier updates.

Recommended Answers

All 3 Replies

declare simply array from object datatype then call AddRange for all combo boxes and pass this array!

As RamyMahrous said use an array. If you deceide to add or remove anything later you only need to do it in one place.

Dim ary As String() = {" single space", "- hyphen", " -  <single space>hyphen<single space>", "` open single quote", "=", "~", "!"}

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        verticalBarComboBox.Items.AddRange(ary)
        backSlashComboBox.Items.AddRange(ary)
        colonComboBox.Items.AddRange(ary)
        quoteComboBox.Items.AddRange(ary)
    End Sub

Thanks to both!

As this is my first VB project, I needed the code snippet.

Be a part of the DaniWeb community

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