Combobox Additions at Run-Time

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

Guest

I have Visual Studio .NET with all of the components installed onto it. I
want to add items to a combo box at run-time through numerous subroutines. I
previously did it with the combo boxes AddItem method, but in .NET's version
it gives me an error, because the method no longer exists. I know I can use
the Items property, but that is Read-Only at run-time.

I previously used Microsoft Visual Basic 6 (not a Visual Studio component)
and it worked fine before, but now the AddItem and RemoveItem methods are
gone.
 
Jan. 13, 2005

No, you can't add items to the collection by using:

dim coll as objectcollection
coll = combobox1.items
coll.add("...")

You can add items at runtime though with:

combobox1.Items.Add("MyItem")

I hope this answers your question and I hope you have a great day!


Joseph MCAD
 
Chaos2651 said:
I have Visual Studio .NET with all of the components installed onto it. I
want to add items to a combo box at run-time through numerous subroutines.
I
previously did it with the combo boxes AddItem method, but in .NET's
version
it gives me an error, because the method no longer exists. I know I can
use
the Items property, but that is Read-Only at run-time.

\\\
Me.ListBox1.Items.Add("Hello World")
///
 
Why do you think this will not work? You are just making a reference to the
collection and then adding items to that collection. It will work.

Dim ComboBox1 As New ComboBox
Dim coll As ComboBox.ObjectCollection
coll = ComboBox1.Items
coll.add("...")
MessageBox.Show(ComboBox1.Items.Count)

Chris
 
Thank you for a quick and speedy reply! I didn't know that the Items property
had subroutines. I'm more of a C++ guy rather than VB.

Thanks!
 
Jan. 13, 2005

Your welcome! And the reason that your code won't work Chris, is
because the collection returned by ComboBox1.Items is readonly.

Dim ComboBox1 As New ComboBox
Dim coll As ComboBox.ObjectCollection
coll = ComboBox1.Items 'This returns a readonly collection and cannot be
added to
coll.add("...")
MessageBox.Show(ComboBox1.Items.Count)

You must add to the collection through the combobox instead. Have a
great day!


Joseph MCAD
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top