Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,913 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,181 people online right now. Registration is fast and FREE... Join Now!




Count property

 
Reply to this topicStart new topic

Count property

raeNet
11 Dec, 2007 - 02:01 PM
Post #1

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
I have an assignment with 5 buttons. I had to code each one to modify items displayed in a list box. I've completed the Insert, Remove, RemoveAt and Clear Methods, but I'm having difficulty with the Count property.

I need to code the associated Count Button to display the number of items in the list box. Can anyone help? Here's what I have:

CODE


Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
        Dim count As Integer
        For count = 0 To Me.xNamesListBox.Items.Count - 1
            Me.xNamesListBox.Text.ToString()

        Next count

    End Sub


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Count Property
11 Dec, 2007 - 02:16 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
If you need a label or textbox just to read the count you can do...

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
   mylabel.text = xNamesListBox.Items.Count
End sub


If like your other example you are trying to print out the values of each item you can do...

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
        Dim counter As Integer = 0

        ' Clear the label before starting
        mylabel.Text = ""

        ' Move through the listbox items from 0 to the count - 1
        For counter = 0 To xNamesListBox.Items.Count - 1

            'Add onto the label the count and the items value
            mylabel.Text &= "Item " & (counter + 1) & " is: " & xNamesListBox.Items(counter).ToString() & ControlChars.CrLf
        Next
End sub


That should be all you need.

Enjoy!

"At DIC we be name listbox counting code ninjas!" decap.gif
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Count Property
11 Dec, 2007 - 02:26 PM
Post #3

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
QUOTE(Martyr2 @ 11 Dec, 2007 - 03:16 PM) *

If you need a label or textbox just to read the count you can do...

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
   mylabel.text = xNamesListBox.Items.Count
End sub


If like your other example you are trying to print out the values of each item you can do...

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
        Dim counter As Integer = 0

        ' Clear the label before starting
        mylabel.Text = ""

        ' Move through the listbox items from 0 to the count - 1
        For counter = 0 To xNamesListBox.Items.Count - 1

            'Add onto the label the count and the items value
            mylabel.Text &= "Item " & (counter + 1) & " is: " & xNamesListBox.Items(counter).ToString() & ControlChars.CrLf
        Next
End sub


That should be all you need.

Enjoy!

"At DIC we be name listbox counting code ninjas!" decap.gif


Thanks - you're quite expedient. But, using the first code required conversion to string so I did that, but nothing is displayed. Any clues?

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
        Me.xNamesListBox.Text = CStr(Me.xNamesListBox.Items.Count)
    End Sub
End Class

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Count Property
11 Dec, 2007 - 02:33 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You must be doing something wrong because the examples I am giving you are correct. You shouldn't have to convert to string explicitly, this will be done automatically. Make sure you have your controls named properly and that you are referring to the proper controls. Are they sized correctly? Are they set to visible = true? Did you add items to the names listbox before trying the button?

Being that you are not getting an outright error is also telling me that you have something visual not working right.

This post has been edited by Martyr2: 11 Dec, 2007 - 02:34 PM
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Count Property
11 Dec, 2007 - 07:06 PM
Post #5

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
QUOTE(raeNet @ 11 Dec, 2007 - 03:26 PM) *

QUOTE(Martyr2 @ 11 Dec, 2007 - 03:16 PM) *

If you need a label or textbox just to read the count you can do...

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
   mylabel.text = xNamesListBox.Items.Count
End sub


If like your other example you are trying to print out the values of each item you can do...

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
        Dim counter As Integer = 0

        ' Clear the label before starting
        mylabel.Text = ""

        ' Move through the listbox items from 0 to the count - 1
        For counter = 0 To xNamesListBox.Items.Count - 1

            'Add onto the label the count and the items value
            mylabel.Text &= "Item " & (counter + 1) & " is: " & xNamesListBox.Items(counter).ToString() & ControlChars.CrLf
        Next
End sub


That should be all you need.

Enjoy!

"At DIC we be name listbox counting code ninjas!" decap.gif


Thanks - you're quite expedient. But, using the first code required conversion to string so I did that, but nothing is displayed. Any clues?

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click
        Me.xNamesListBox.Text = CStr(Me.xNamesListBox.Items.Count)
    End Sub
End Class


==================

There are still build errors? I'm attaching the entire zip file for you to look at, if that's possible?

Thanks in advance.











Attached File(s)
Attached File  Items_Solution.zip ( 54.54k ) Number of downloads: 42
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Count Property
11 Dec, 2007 - 10:05 PM
Post #6

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
You need to output the results to a label, not a listbox. How many times do I have to say this. The reason you are not seeing anything is because you are attempting to set a listbox when you need to set a label or a textbox. If you are absolutely bent on adding the count to the listbox you have to do it like this...

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click

      Me.xNamesListBox.Items.Add(Me.xNamesListBox.Items.Count.ToString())

End Sub


That code will add the count as one of the items to the listbox. Hit it again and it will add the count again. You should probably put the output to a label or textbox though.
User is offlineProfile CardPM
+Quote Post

raeNet
RE: Count Property
11 Dec, 2007 - 10:34 PM
Post #7

D.I.C Head
**

Joined: 1 Nov, 2007
Posts: 115


My Contributions
QUOTE(Martyr2 @ 11 Dec, 2007 - 11:05 PM) *

You need to output the results to a label, not a listbox. How many times do I have to say this. The reason you are not seeing anything is because you are attempting to set a listbox when you need to set a label or a textbox. If you are absolutely bent on adding the count to the listbox you have to do it like this...

CODE

Private Sub xCountButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xCountButton.Click

      Me.xNamesListBox.Items.Add(Me.xNamesListBox.Items.Count.ToString())

End Sub


That code will add the count as one of the items to the listbox. Hit it again and it will add the count again. You should probably put the output to a label or textbox though.

================

Thank you - I see your point. My directions were vague. They did not specify where or how to display the result; therefore, I assumed it was in the listbox. I agree with your advice and will display in an alternate way. You've been most helpful and I truly appreciate your attention and prompt response. THX!

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:07PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month