4 Replies - 6384 Views - Last Post: 05 August 2012 - 07:40 AM Rate Topic: -----

#1 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Count total number of items in listbox?

Posted 04 August 2012 - 06:36 AM

Hi again, sorry for another post asking a question, I am still learning. I need to count up the total number of items in a listbox and display that number in a text box. I have tried
lstFTotal.Text = lstFDisplay.Items.Count


I get no errors but no output either.
Is This A Good Question/Topic? 0
  • +

Replies To: Count total number of items in listbox?

#2 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: Count total number of items in listbox?

Posted 04 August 2012 - 08:41 AM

You are looking at the right properterty (.Count)

So your problem is elsewhere.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler



Was This Post Helpful? 1
  • +
  • -

#3 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Count total number of items in listbox?

Posted 05 August 2012 - 07:05 AM

Thanks for the advice, I read the section on debugging and using a breakpoint I was able to discover that the code stops working before it actually gets to the line
lstFTotal.Text = lstFDisplay.Items.Count
but I am not sure why.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,354
  • Joined: 02-June 10

Re: Count total number of items in listbox?

Posted 05 August 2012 - 07:33 AM

Well we only have one line to go on, so I don't think there is anymore we can do for you.

Code doesn't just 'stop'. It either crashes on a line and gives you an error, or the method completes as it is programmed to. Note I say "programmed to" and not "as you THOUGHT you programmed it to".

It is possible you are calling a method that just never returns. That doesn't mean the code stopped. It just means you hit F10 on the line calling the other method (that doesn't return.) When it gets to that line try hitting F11 which as you can see is "jump into" instead of "jump over". That will cause the debugger to follow into the method being called. Maybe you can then see why it never returns. Keep in mind this is a HUGE guess on my part having so little to go on.

This post has been edited by tlhIn`toq: 05 August 2012 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 reggina  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 02-August 12

Re: Count total number of items in listbox?

Posted 05 August 2012 - 07:40 AM

I didn't want to post my whole code as I would have preferred to try and work this out myself if I can, but I am just struggling to. It is stopping on waiting.Close() and I have jumped pasted this line and I get no error or output again. Thank you for trying to assist me and for giving me resources to read to try and help improve myself.


Imports System.IO

Public Class Form1
    Dim firstname(20), lastname(20), applicationdate(40), address(40), gender(40), M, F As String
    Dim totalM, totalF, grandtotal, loops As Single
    Dim itemcount As Integer


    Private Sub btnRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRead.Click
        dlgImport.ShowDialog()
        Dim waiting As StreamReader = File.OpenText(dlgImport.fileName)

        Do While waiting.Peek <> -1
            firstname(itemcount) = waiting.ReadLine()
            lastname(itemcount) = waiting.ReadLine()
            gender(itemcount) = waiting.ReadLine()
            applicationdate(itemcount) = waiting.ReadLine()
            address(itemcount) = waiting.ReadLine()
            If gender(itemcount) = "M" Then
                lstMDisplay.Items.Add(firstname(itemcount))
                lstMDisplay.Items.Add(lastname(itemcount))
                lstMDisplay.Items.Add(gender(itemcount))
                lstMDisplay.Items.Add(applicationdate(itemcount))
                lstMDisplay.Items.Add(address(itemcount))
                itemcount = itemcount + 1
            ElseIf gender(itemcount) = "F" Then
                lstFDisplay.Items.Add(firstname(itemcount))
                lstFDisplay.Items.Add(lastname(itemcount))
                lstFDisplay.Items.Add(gender(itemcount))
                lstFDisplay.Items.Add(applicationdate(itemcount))
                lstFDisplay.Items.Add(address(itemcount))
                itemcount = itemcount + 1
            End If
        Loop
        waiting.Close()
    End Sub

    Private Sub lstFTotal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstFTotal.SelectedIndexChanged
        lstFTotal.Text = lstFDisplay.Items.Count
    End Sub


    Private Sub btnGTotal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGTotal.Click
        lstGTotal.Text = Val(lstMTotal.Text) + Val(lstFTotal.Text)
    End Sub

    Private Sub lstGTotal_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstGTotal.SelectedIndexChanged
        lstGTotal.Text = Val(lstMTotal.Text) + Val(lstFTotal.Text)
    End Sub

    Private Sub btnMFTotals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMFTotals.Click
        Dim count As Integer = lstFDisplay.Items.Count()
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1