[RESOLVED] Change listview Items Forecolor
I am trying to change a listviews items forecolor after searching through the listview and finding a entry that matches a textbox's text
I can perform the listview search but i cannot get the listviews text forecolor to change color
Code:
Dim text As String = Me.TextBox1.Text
For i As Integer = 0 To Me.ListView1.Items.Count - 1
Me.ListView1.FindItemWithText(text) '.ForeColor = Color.Blue
Me.ListView1.SelectedItems(i).ForeColor = Color.Blue
Next
error message on this line "Me.ListView1.SelectedItems(i).ForeColor = Color.Blue":
Quote:
InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index
regards
toe
Re: Change listview Items Forecolor
The error message is telling you that 0 is an invalid index. What are you indexing? The SelectedItems collection. So, if 0 is not a valid index, what does that tell you?
Apart from that, your code does really make sense. Why would you be looping through all the items in the ListView and calling FindItemWithText for every one when you're using the same text every time so the result will be the same every time? Why do you need to call FindItemWithText more than once? Why do you need the loop at all?
Why are you calling FindItemWithText and then simply ignoring the result? FindItemWithText returns the item it finds. The idea would be that you do something with that item, like perhaps setting its ForeColor.
Why are you trying to set the ForeColor of an item in the SelectedItems collection when that has nothing to do with the item that you just found?
Have you read the documentation for the FindItemWithText method? One of these days I'll stop asking that question and just assume that noone really wants to make an effort to find the answer for themselves. That documentation includes a code example that shows you how to use it. Please, read the documentation first in future.
Re: Change listview Items Forecolor
I am trying to make a app that will check my lotto numbers and then change the forecolor of the number if found.
After searching almost all day that is the closet thing i could come up with.
Re: Change listview Items Forecolor
Have you read the documentation for the FindItemWithText method yet? This is from the code example:
vb.net Code:
' Call FindItemWithText, sending output to MessageBox.
Dim item1 As ListViewItem = findListView.FindItemWithText("brack")
If (item1 IsNot Nothing) Then
MessageBox.Show("Calling FindItemWithText passing 'brack': " _
& item1.ToString())
Else
MessageBox.Show("Calling FindItemWithText passing 'brack': null")
End If
As that code plainly shows, you get the ListViewItem that FindItemWithText returns, test whether it's Nothing and, if it's not, use it. In your case, "use it" means set it's ForeColor. If you know for a fact that the text you're looking for exists then the test is not required.
Re: Change listview Items Forecolor
I carnt work it out so i have found some code
Code:
'http://vbcity.com/forums/topic.asp?tid=149297&highlight=search|listview
'items
Public Function FindListViewItemByText(ByVal listViewItemText As String) As ListViewItem
Dim i As Integer
' Loop through ListViewItems.
For i = 1 To Me.ListView1.Items.Count - 1
' If found, return a reference to the ListViewItem.
If Me.ListView1.Items(i).Text = Me.TextBox1.Text Then
Return Me.ListView1.Items(i)
End If
Next
' If not found return Nothing.
Return Nothing
End Function
' SubItem
Public Function FindListViewSubItemByText(ByVal listViewItemText As String, ByVal listViewSubItemText As String) As ListViewItem.ListViewSubItem
Dim i As Integer
' Loop through ListViewItems.
For i = 1 To Me.ListView1.Items.Count - 1
If Me.ListView1.Items(i).Text = listViewItemText Then
Dim ii As Integer
' Loop through the ListViewItem’s ListViewSubItems.
For ii = 1 To Me.ListView1.Items(i).SubItems.Count - 1
' If found, return a reference to the ListViewSubItem.
If Me.ListView1.Items(i).SubItems(ii).Text = Me.TextBox1.Text Then
Return Me.ListView1.Items(i).SubItems(ii)
End If
Next
End If
Next
' If not found return Nothing.
Return Nothing
End Function
But now i just have to learn how to call a public function
regards
toe
Re: Change listview Items Forecolor
I really hope you were joking with that last post...
Just write the function's name and it'll get called, it's as simple as that...
And I'd try www.homeandlearn.co.uk for some VB.NET tutorials if I were you.
Re: Change listview Items Forecolor
I have called a public sub and a module by just typing its name but when i tried it on a this public Function it produced a error so i dont think its as simple as just typing its name.
Looking at the lesson link you have posted i think i will be able to work it out as there is a good example on calling a public function
thanks for the link
toe
Re: Change listview Items Forecolor
Well this public function takes a parameter, so you need to pass a suitable argument when you call it. Otherwise it's just as I said, writing the name calls the function. Trust me, will ya! I have written over 50.000 lines of code, I think I know how to call a function... Don't be so dismissive of other ppl's advice... Most of the ppl here know what they're talking about.
Btw, when you do something and it produces an error, the error is not just a nuisance, it tells you why it happened and how to prevent it from happening again. Did you look at the error or did you just run away from it?
Re: Change listview Items Forecolor
obi, iam not questioning your coding ability.
As you said most people here know what they are talking about but some dont. eg me thats why iam here.
Simply saying its easy, just write its name didnt help at all, , i tried that before i posted, amongst other things.
Re: Change listview Items Forecolor
Quote:
Originally Posted by jmcilhinney
Have you read the documentation for the FindItemWithText method yet? This is from the code example:
vb.net Code:
' Call FindItemWithText, sending output to MessageBox.
Dim item1 As ListViewItem = findListView.FindItemWithText("brack")
If (item1 IsNot Nothing) Then
MessageBox.Show("Calling FindItemWithText passing 'brack': " _
& item1.ToString())
Else
MessageBox.Show("Calling FindItemWithText passing 'brack': null")
End If
As that code plainly shows, you get the ListViewItem that FindItemWithText returns, test whether it's Nothing and, if it's not, use it. In your case, "use it" means set it's ForeColor. If you know for a fact that the text you're looking for exists then the test is not required.
I have tried the example you posted and replaced the ("brack") with (me.textbox1.text) which is a 5.
The code runs throught the 12 lines in the listview, changes the forecolor of the first row it finds with a five and misses the remaing 2 lines that also have a 5 as a subitem.
Code:
For i As Integer = 0 To Me.ListView1.Items.Count - 1
Dim item1 As ListViewItem = Me.ListView1.FindItemWithText(Me.TextBox1.Text, True, 0)
If (item1 IsNot Nothing) Then
item1.ForeColor = Color.Blue
End If
If (item1.SubItems IsNot Nothing) Then
item1.SubItems(1).ForeColor = Color.Blue
End If
Next
The help says it is supposed to include the subitems if i set the 2nd parameter to true but it seems to ingnore it.
Also i am not trying to change the rows forecolor, just the matching numbers forecolor which this line does " item1.ForeColor = Color.Blue"
So i have to ask, can one just change a single items forecolor or only the entire row?
regards
toe
Re: Change listview Items Forecolor
Try this variation, the 3rd index is supposed to be the index where you want to start, FindItemWithText returns the first match so it should always be the same if you are not changing the index.
VB.Net Code:
For i As Integer = 0 To Me.ListView1.Items.Count - 1
Dim item1 As ListViewItem = Me.ListView1.FindItemWithText(Me.TextBox1.Text, True, i)
If (item1 IsNot Nothing) Then
item1.ForeColor = Color.Blue
End If
If (item1.SubItems IsNot Nothing) Then
item1.SubItems(1).ForeColor = Color.Blue
End If
Next
You can change the color per subitem if you wish.
Re: Change listview Items Forecolor
dee-u
I have tried your code with a few different variations and it gets a error on the subitem line
Code:
If (item1.SubItems IsNot Nothing) Then
Object reference not set to an instance of an object.
regards
toe
Re: Change listview Items Forecolor
Could you try something like this also?
VB.Net Code:
Private Sub button1_Click_1(ByVal sender As Object, ByVal e As EventArgs)
Dim a As Integer = 0
Dim li As ListViewItem = listView1.FindItemWithText(Me.TextBox1.Text, True, a)
While (li IsNot Nothing) AndAlso (a < listView1.Items.Count - 1)
a = li.Index + 1 'start searching after the index of the list item found
li.ForeColor = Color.Blue
li = listView1.FindItemWithText(Me.TextBox1.Text, True, a)
End While
End Sub
Re: Change listview Items Forecolor
The ListView.FindItemWithText Method (String, Boolean, Int32) description from MSDN seem to be confusing.
Quote:
Originally Posted by MSDN
Finds the first ListViewItem or ListViewItem.ListViewSubItem, if indicated, that begins with the specified text value. The search starts at the specified index
Quote:
Originally Posted by MSDN
Return Value
Type: System.Windows.Forms.ListViewItem
The first ListViewItem that begins with the specified text value.
I am not sure how to determine if the item returned by the function is ListViewItem or ListViewItem.ListViewSubItem, sorry, I cannot help you on that regard.
In any case, if you want to change the forecolor of a subitem you must set this property to the ListViewItem.
Code:
li.UseItemStyleForSubItems = false
Re: Change listview Items Forecolor
ok thanks for your help.
I give up on this one, if your having problems i got no chance :)
regards
toe
Re: Change listview Items Forecolor
Don't give up just yet, I have an idea, after retrieving the ListVIewItem you oculd actually try to enumerate its SubItems to determine which one has the target string, it should be easy.
Re: Change listview Items Forecolor
Ok pal, have a look at this, have fun!
Vb.Net Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer = 0
Dim li As ListViewItem = lvwListView1.FindItemWithText(Me.TextBox1.Text, True, a)
'continue searching while there is a match
While (li IsNot Nothing)
a = li.Index + 1 'start searching after the index of the list item found
li.UseItemStyleForSubItems = False
ColorItems(li, Me.TextBox1.Text) 'color the column where the string was found
If (a = lvwListView1.Items.Count) Then
'we have reach the last item, exit now
Exit While
End If
li = lvwListView1.FindItemWithText(Me.TextBox1.Text, True, a)
End While
End Sub
Private Sub ColorItems(ByVal li As ListViewItem, ByVal text As String)
If (li.SubItems.Count > 0) Then
Dim a As Int32 = 0
For a = 0 To li.SubItems.Count - 1
If (li.SubItems(a).Text.IndexOf(text)) = 0 Then
li.SubItems(a).ForeColor = Color.Blue
End If
Next
End If
End Sub
Re: Change listview Items Forecolor
Thats perfect, thank you very much
Re: [RESOLVED] Change listview Items Forecolor