Results 1 to 12 of 12

Thread: listview1 colums and items

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    312

    listview1 colums and items

    Hey all, ive had to start a new thread since intentions has changed since the original one, and things got confussing

    I have a listview called listview1

    listview1 has 4 colums already added, wich i added via properties

    there are 4 colums and there names and colums are

    Avator = colum 0
    Username = colum 1
    Status = colum 2
    Game = colum 3

    Listbox2 is full of items, each item is full with text.

    what i want to do is do a search for usernames, and add each one to listview1 under colum 1

    currently, i am finding usernames via text in a textbox called textbox 1 and adding it to a listbox called listbox1 ussing the following code.

    Code:
    Private Function GetTagContents(ByVal Source As String, ByVal startTag As String, ByVal endTag As String) As List(Of String)
            startTag = startTag.ToUpper()
            endTag = endTag.ToUpper()
            Dim StringsFound As New List(Of String)
            Dim Index As Integer = Source.ToUpper.IndexOf(startTag) + startTag.Length
    
            While Index <> startTag.Length - 1
    
                StringsFound.Add(Source.Substring(Index, Source.ToUpper.IndexOf(endTag, Index) - Index))
                Index = Source.ToUpper.IndexOf(startTag, Index) + startTag.Length
    
         
    
            End While
            Return StringsFound
    
        End Function
    With

    Code:
         ListBox1.Items.AddRange(GetTagContents(TextBox1.Text, "<SPAN class=friend_id>", "</span>").ToArray)

    But what i need to do is to go through every item in listbox2, find the username and add it to the username colum (1)

    Please help

    I hope i made more sence this time
    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: listview1 colums and items

    What is in Listbox2?

    I can't tell you how to find the text for the columns, so I'll invent functions for them. You'll have to add the code for them manually, as you didn't tell us where you get them from.
    I am assuming all the information is in each item of ListBox2, and each item contains one username, one avator, one... etc.

    vb.net Code:
    1. Dim lvi As ListViewItem
    2. Dim strAvator, strUsername, strStatus, strGame As String
    3.  
    4. 'Loop through Listbox2 items
    5. For Each item As String In ListBox2.Items
    6.  
    7.    'Get the four texts to put in the ListView
    8.    strAvator = GetAvator(item)
    9.    strUserName = GetUsername(item)
    10.    strStatus = GetStatus(item)
    11.    strGame = GetGame(item)
    12.  
    13.    'Create a new ListViewItem with 3 sub-items, based on these strings
    14.    'The order that you add them is also the order they appear
    15.    'so make sure the order matches the order of the columns
    16.    lvi = New ListViewItem
    17.    lvi.Text = strAvator
    18.    lvi.SubItems.Add(strUsername)
    19.    lvi.SubItems.Add(strStatus)
    20.    lvi.SubItems.Add(strGame)
    21.  
    22.    'Add it to the listview
    23.    ListView1.Items.Add(lvi)
    24. Next

    I am assuming the GetUsername function would resemble the GetTagContents function, only that function can return multiple matches, so that doesn't quite fit...?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    312

    Re: listview1 colums and items

    Quote Originally Posted by NickThissen View Post
    What is in Listbox2?

    I can't tell you how to find the text for the columns, so I'll invent functions for them. You'll have to add the code for them manually, as you didn't tell us where you get them from.
    I am assuming all the information is in each item of ListBox2, and each item contains one username, one avator, one... etc.

    vb.net Code:
    1. Dim lvi As ListViewItem
    2. Dim strAvator, strUsername, strStatus, strGame As String
    3.  
    4. 'Loop through Listbox2 items
    5. For Each item As String In ListBox2.Items
    6.  
    7.    'Get the four texts to put in the ListView
    8.    strAvator = GetAvator(item)
    9.    strUserName = GetUsername(item)
    10.    strStatus = GetStatus(item)
    11.    strGame = GetGame(item)
    12.  
    13.    'Create a new ListViewItem with 3 sub-items, based on these strings
    14.    'The order that you add them is also the order they appear
    15.    'so make sure the order matches the order of the columns
    16.    lvi = New ListViewItem
    17.    lvi.Text = strAvator
    18.    lvi.SubItems.Add(strUsername)
    19.    lvi.SubItems.Add(strStatus)
    20.    lvi.SubItems.Add(strGame)
    21.  
    22.    'Add it to the listview
    23.    ListView1.Items.Add(lvi)
    24. Next

    I am assuming the GetUsername function would resemble the GetTagContents function, only that function can return multiple matches, so that doesn't quite fit...?
    Yh, each item in listbox2 contains only one username one avator etc . .

    though the code can find more than one, it wont.

    The GetTagContents is being ussed to find everything, the username, avator etc.

    just being called on diffrently, e.g. to find username its doing

    PHP Code:
    ListBox1.Items.AddRange(GetTagContents(TextBox1.Text"<SPAN class=friend_id>""</span>").ToArray
    to find statusits doing

    Code:
     ListBox3.Items.AddRange(GetTagContents(TextBox1.Text, "<SPAN class=last_online_time>", "</span>").ToArray)
    of course here its looking in textbox1 and getting the text, i need to get it to add to the correct colum

    i want each row of listview1 to be for the text of one item in listbox2

    if you could show me how to loop through the items of listbox2 find the username, add it to the username collum, find the status, add it to the status colum then i could probably work out the rest for my self, and the problom would be solved

    Thanks

    Paul
    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listview1 colums and items

    could you post your TextBox1.Text?

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listview1 colums and items

    the only way to add to your listview is to add to all of the columns at the same time, or your listviewitems won't be synchronized. i'm trying to write you an example but i need the source (textbox1.text) to test it with

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: listview1 colums and items

    The code I posted above should work just fine. Just replace the GetAvator function with the GetTagContents call with the correct start and end tags. Replace the GetUsername function with the GetTagContents call with the other start/end tags, etc...
    Only, the GetTagContents function returns a List(Of String); you only need the first result.

    So that part would become:
    vb.net Code:
    1. strAvator = GetTagContents(item, <avator start tag>, <avator end tag>).Item(0)
    2. strUsername = GetTagContents(item, "<SPAN class=friend_id>", "</SPAN>").Item(0)
    3. strStatus = GetTagContents(item, "<SPAN class=last_online_time>", "</SPAN>").Item(0)
    4. strGame = GetTagContents(item, <game start tag>, <game end tag>).Item(0)

    Just replace the start/end tags you did not tell us yet with the correct ones.
    It would also be wise to do some error checking. What if the current item doesn't contain the Status tag for example? Right now, my code is trying to access Item(0) of the GetTagContents function, which would not exist and throw an error.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    312

    Re: listview1 colums and items

    Quote Originally Posted by .paul. View Post
    the only way to add to your listview is to add to all of the columns at the same time, or your listviewitems won't be synchronized. i'm trying to write you an example but i need the source (textbox1.text) to test it with
    ok got told a way to do it, just need to do my question on post 8
    Last edited by Paul_so40; Aug 17th, 2009 at 07:17 PM.
    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    312

    Re: listview1 colums and items

    Quote Originally Posted by NickThissen View Post
    The code I posted above should work just fine. Just replace the GetAvator function with the GetTagContents call with the correct start and end tags. Replace the GetUsername function with the GetTagContents call with the other start/end tags, etc...
    Only, the GetTagContents function returns a List(Of String); you only need the first result.

    So that part would become:
    vb.net Code:
    1. strAvator = GetTagContents(item, <avator start tag>, <avator end tag>).Item(0)
    2. strUsername = GetTagContents(item, "<SPAN class=friend_id>", "</SPAN>").Item(0)
    3. strStatus = GetTagContents(item, "<SPAN class=last_online_time>", "</SPAN>").Item(0)
    4. strGame = GetTagContents(item, <game start tag>, <game end tag>).Item(0)

    Just replace the start/end tags you did not tell us yet with the correct ones.
    It would also be wise to do some error checking. What if the current item doesn't contain the Status tag for example? Right now, my code is trying to access Item(0) of the GetTagContents function, which would not exist and throw an error.
    how can i make it so that if it cant find one, it will just put it as "could not find" ?
    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

  9. #9
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: listview1 colums and items

    The easiest way is probably to change the GetTagContents function. Right now it is designed to find all the occurrences, but you only need one.

    Something like this should work:
    vb.net Code:
    1. Private Function GetTagContents(ByVal Source As String, ByVal startTag As String, ByVal endTag As String) As String
    2.     Dim result As String = "Not found."
    3.  
    4.     startTag = startTag.ToUpper()
    5.     endTag = endTag.ToUpper()
    6.  
    7.     Dim startIndex As Integer = Source.ToUpper.IndexOf(startTag)
    8.     If startIndex <> -1 Then
    9.         startIndex += startTag.Length
    10.  
    11.         Dim endIndex As Integer = Source.Toupper.IndexOf(endTag, startIndex + 1)
    12.         If endIndex <> -1 Then
    13.             result = Source.SubString(startIndex, endIndex - startIndex)
    14.         Else
    15.             'No end tag found
    16.             result = "No end tag found."
    17.         End If
    18.     Else
    19.         'No start tag found
    20.         result = "No start tag found."
    21.     End If
    22.  
    23.     Return result
    24. End Function

    Now, it returns the tag if it's found. If the start tag is not found it says "No start tag found.", and if the end tag isn't found it says "No end tag found.". If you only want it to say simply "Not found." then you can remove the two Else blocks. The result String is initialized as "Not found.", so if you If blocks do not pass (the statement is false), the result string is never changed, and the function returns "Not found.".

    Note that the function now returns a String, rather than a List(Of String). It can only return one instance of your tag (the first) and will ignore all others.
    So, you do not need the "Item(0)" behind the function; it returns the string you need already.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    312

    Re: listview1 colums and items

    Ok thanks,

    I have had to rename it to getinfo because alawise it collides with the other function, that needs to find more than one.

    but when ussing this

    strUsername = Getinfo(item, "<SPAN class=friend_id>", "</SPAN>").Item(0)
    the bit in blue getts highlighted and gives error

    Item is not a member of string


    What is going wrong? i am ussing the function you gave me with the only change being changing the function name to getinfo
    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

  11. #11
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: listview1 colums and items

    Quote Originally Posted by NickThissen View Post
    Note that the function now returns a String, rather than a List(Of String). It can only return one instance of your tag (the first) and will ignore all others.
    So, you do not need the "Item(0)" behind the function; it returns the string you need already.
    Did you read that?

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    312

    Re: listview1 colums and items

    Quote Originally Posted by NickThissen View Post
    Did you read that?
    Yh i did, then forgot to remove them lol

    Thanks

    Paul
    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width