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 :)
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:
Dim lvi As ListViewItem
Dim strAvator, strUsername, strStatus, strGame As String
'Loop through Listbox2 items
For Each item As String In ListBox2.Items
'Get the four texts to put in the ListView
strAvator = GetAvator(item)
strUserName = GetUsername(item)
strStatus = GetStatus(item)
strGame = GetGame(item)
'Create a new ListViewItem with 3 sub-items, based on these strings
'The order that you add them is also the order they appear
'so make sure the order matches the order of the columns
lvi = New ListViewItem
lvi.Text = strAvator
lvi.SubItems.Add(strUsername)
lvi.SubItems.Add(strStatus)
lvi.SubItems.Add(strGame)
'Add it to the listview
ListView1.Items.Add(lvi)
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...?
Re: listview1 colums and items
Quote:
Originally Posted by
NickThissen
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:
Dim lvi As ListViewItem
Dim strAvator, strUsername, strStatus, strGame As String
'Loop through Listbox2 items
For Each item As String In ListBox2.Items
'Get the four texts to put in the ListView
strAvator = GetAvator(item)
strUserName = GetUsername(item)
strStatus = GetStatus(item)
strGame = GetGame(item)
'Create a new ListViewItem with 3 sub-items, based on these strings
'The order that you add them is also the order they appear
'so make sure the order matches the order of the columns
lvi = New ListViewItem
lvi.Text = strAvator
lvi.SubItems.Add(strUsername)
lvi.SubItems.Add(strStatus)
lvi.SubItems.Add(strGame)
'Add it to the listview
ListView1.Items.Add(lvi)
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
Re: listview1 colums and items
could you post your TextBox1.Text?
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
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:
strAvator = GetTagContents(item, <avator start tag>, <avator end tag>).Item(0)
strUsername = GetTagContents(item, "<SPAN class=friend_id>", "</SPAN>").Item(0)
strStatus = GetTagContents(item, "<SPAN class=last_online_time>", "</SPAN>").Item(0)
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.
Re: listview1 colums and items
Quote:
Originally Posted by
.paul.
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
Re: listview1 colums and items
Quote:
Originally Posted by
NickThissen
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:
strAvator = GetTagContents(item, <avator start tag>, <avator end tag>).Item(0)
strUsername = GetTagContents(item, "<SPAN class=friend_id>", "</SPAN>").Item(0)
strStatus = GetTagContents(item, "<SPAN class=last_online_time>", "</SPAN>").Item(0)
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" ?
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:
Private Function GetTagContents(ByVal Source As String, ByVal startTag As String, ByVal endTag As String) As String
Dim result As String = "Not found."
startTag = startTag.ToUpper()
endTag = endTag.ToUpper()
Dim startIndex As Integer = Source.ToUpper.IndexOf(startTag)
If startIndex <> -1 Then
startIndex += startTag.Length
Dim endIndex As Integer = Source.Toupper.IndexOf(endTag, startIndex + 1)
If endIndex <> -1 Then
result = Source.SubString(startIndex, endIndex - startIndex)
Else
'No end tag found
result = "No end tag found."
End If
Else
'No start tag found
result = "No start tag found."
End If
Return result
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.
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
Quote:
strUsername = Getinfo(item, "<SPAN class=friend_id>", "</SPAN>").Item(0)
the bit in blue getts highlighted and gives error
Quote:
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
Re: listview1 colums and items
Quote:
Originally Posted by
NickThissen
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?
Re: listview1 colums and items
Quote:
Originally Posted by
NickThissen
Did you read that?
Yh i did, then forgot to remove them lol
Thanks
Paul