Count total number in listbox?

Senoska

Member
Joined
Jun 27, 2006
Messages
15
Programming Experience
Beginner
Hi again, sorry for another post asking a question ^^; still learning..
I need to get a total number of items in a listbox and display then in a text box :O any idea on what to do?

I tryed

lstInfo.Items.Count(txtTotalMem.text)

but ive got nothing, I tryed google with prob 100 differnt things :( still no luck ;; please help! thanks alot people ^^b :p
 
if you want to just put the number of items the listbox has into a textbox:
VB.NET:
        TextBox1.Text = ListBox1.Items.Count

If you want to add each item thats in the listbox into the textbox you could do something like:

VB.NET:
        Dim myStr As String
        For a As Integer = 0 To ListBox1.Items.Count - 1
            myStr += ListBox1.Items.Item(a) + " "
        Next
        TextBox1.Text = myStr.Trim
 
I tryed :

txtTotalMem.Text = lstInfo.Items.Count

but its giving me 0 when I have 13 items in the box at the moment :( any idea?
 
even on bound controls, once the data has been bound and the control is populated, then list.count returns the number of items on display.
you can ask the datasource for how many items it has.. if the datasource is a datatable, then dt.Rows.Count will give it to you, if the source is a bindingsource then bs.List.Count will give it to you..



but do be aware of when you actually set this.. if you do this:

txtTotalMem.Text = lstInfo.Items.Count

in the form load, when the list has 0 items.. then you add items, it wont change the 0. you can attach to the listchanged event of the box, and update the count at that time
 
Back
Top