List box Items..how do you refer to them?  
Author Message
xRuntime





PostPosted: Visual Basic Language, List box Items..how do you refer to them? Top

I wanted to refer to a specific listbox item....but how do you do that

I tried using the same method as one would do with tabs, ex.

Listbox1.Items.Item(1)

Using an index number...
but that didnt work apparently, I got an exception error...

So how would I refer to the first item in a listbox, the second

I cant use their specific name because I have code set to change the listbox items at user intervention...



Visual Basic16  
 
 
Moayad Mardini





PostPosted: Visual Basic Language, List box Items..how do you refer to them? Top

The first item is Listbox1.Items.Item(0) not (1).


 
 
Geek Squad





PostPosted: Visual Basic Language, List box Items..how do you refer to them? Top

I tried that already. It gave me an exception error. So I changed it to 1. Still got the exception error. So I posted here.


 
 
scottwis_MS





PostPosted: Visual Basic Language, List box Items..how do you refer to them? Top

What exception did you get

Is the list empty when you pass in 0 If there are no items in the list, then you can't access item 0. Try checking the count first.

 
 
Geek Squad





PostPosted: Visual Basic Language, List box Items..how do you refer to them? Top

alright here's whats going on:

on form_load I have the following

Listbox1.Items.Item(0)= My.Settings.firstitem.ToString()

(i tried w/o tostring too)

By Default My.Settings.firstitem has the text "blank"

It gives me an exception error you cannot refer to the index with the number 0


 
 
scottwis_MS





PostPosted: Visual Basic Language, List box Items..how do you refer to them? Top

You are getting this error because the list box is empty, which means there is no item 0. You have to add an item to the list box using Listbox1.Items.Add

-Scott


 
 
Geek Squad





PostPosted: Visual Basic Language, List box Items..how do you refer to them? Top

Hmm what a simple answer....I should use the add function instead, right


 
 
scottwis_MS





PostPosted: Visual Basic Language, List box Items..how do you refer to them? Top

Yes. If you want to add an item to the list box, then you need to use the Add method.