[Help]adding items to the listview code

Posts 18 of 8 · Page 1 of 1
[Help]adding items to the listview code

guys. I need some help here.

if I want to add something to the listbox by this code
Code:
    Public Sub Add_Item( _
    ByVal Moderator As String, _
    ByVal User As String, _
    ByVal Type As String, _
    ByVal Points As String, _
    ByVal Reason As String)
        Dim Count As Integer = 0
        ListView1.Items.Add(Moderator)
        ListView1.Items(Count).SubItems.Add(User)
        ListView1.Items(Count).SubItems.Add(Type)
        ListView1.Items(Count).SubItems.Add(Points)
        ListView1.Items(Count).SubItems.Add(Reason)
        Count += 1
    End Sub
it just appears as 1 and it can't add other items no longer..
could someone solve this problem... thanks.
Where's the count function for ?
Why you dont just use
listview.items.add("") ?
Quote Originally Posted by confict View Post
Where's the count function for ?
Why you dont just use
listview.items.add("") ?
There is no count "function" there, and this sub is to simplify work obviously.

@OP
Okay first off, your logic is all wrong for the count variable.
[php]
Dim Count As Integer = 0
ListView1.Items.Add(Moderator)
ListView1.Items(Count).SubItems.Add(User)
ListView1.Items(Count).SubItems.Add(Type)
ListView1.Items(Count).SubItems.Add(Points)
ListView1.Items(Count).SubItems.Add(Reason)
Count += 1
[/php]

Let's see...every time you call the sub you're declaring an integer with a value of 0, so it's going be 0 all the time...

Instead try:

[php]
Public Sub Add_Item( _
ByVal Moderator As String, _
ByVal User As String, _
ByVal Type As String, _
ByVal Points As String, _
ByVal Reason As String)
Dim Count As Int32 = Me.ListView1.Items.Count-1
ListView1.Items.Add(Moderator)
ListView1.Items(Count).SubItems.Add(User)
ListView1.Items(Count).SubItems.Add(Type)
ListView1.Items(Count).SubItems.Add(Points)
ListView1.Items(Count).SubItems.Add(Reason)
End Sub
[/php]

That should work.

EDIT: For the sake of a better explanation + I'm bored:

By changing count to "Me.ListBiew1.Items.Count-1" you've assigned the variable "count" to a dynamic value that will change as more items are added, rather than a static "0" which will never change which resulted in you finding it kept overwriting previous entries right?
Quote Originally Posted by Jason View Post


There is no count "function" there, and this sub is to simplify work obviously.

@OP
Okay first off, your logic is all wrong for the count variable.
[php]
Dim Count As Integer = 0
ListView1.Items.Add(Moderator)
ListView1.Items(Count).SubItems.Add(User)
ListView1.Items(Count).SubItems.Add(Type)
ListView1.Items(Count).SubItems.Add(Points)
ListView1.Items(Count).SubItems.Add(Reason)
Count += 1
[/php]

Let's see...every time you call the sub you're declaring an integer with a value of 0, so it's going be 0 all the time...

Instead try:

[php]
Public Sub Add_Item( _
ByVal Moderator As String, _
ByVal User As String, _
ByVal Type As String, _
ByVal Points As String, _
ByVal Reason As String)
Dim Count As Int32 = Me.ListView1.Items.Count-1
ListView1.Items.Add(Moderator)
ListView1.Items(Count).SubItems.Add(User)
ListView1.Items(Count).SubItems.Add(Type)
ListView1.Items(Count).SubItems.Add(Points)
ListView1.Items(Count).SubItems.Add(Reason)
End Sub
[/php]

That should work.

EDIT: For the sake of a better explanation + I'm bored:

By changing count to "Me.ListBiew1.Items.Count-1" you've assigned the variable "count" to a dynamic value that will change as more items are added, rather than a static "0" which will never change which resulted in you finding it kept overwriting previous entries right?
Thanks for the explanation
I never worked with listview so I just gave it a try ^^
[php] Dim Count As Int = 0
ListView1.Items.Add(Moderator)
Count = ListView1.Items.Count-1
ListView1.Items(Count).SubItems.Add(User)
ListView1.Items(Count).SubItems.Add(Type)
ListView1.Items(Count).SubItems.Add(Points)
ListView1.Items(Count).SubItems.Add(Reason) [/php]

Else, the first count will be -1.
Just create the item variable, add subitems to it.. and then add that item to the list... :\ no need for all that stuff
Oops thanks for that little error fix Kevin, turns out I had the the fail logic.
thanks for the codes guys. I just tried to create my program for managing a forum because I'm also a moderator of it. so I want to record my status on it ^^

*Solved
Posts 18 of 8 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Need help?