Results 1 to 19 of 19

Thread: [RESOLVED] Change listview Items Forecolor

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Resolved [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":
    InvalidArgument=Value of '0' is not valid for 'index'.
    Parameter name: index
    regards

    toe

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Change listview Items Forecolor

    Have you read the documentation for the FindItemWithText method yet? This is from the code example:
    vb.net Code:
    1. ' Call FindItemWithText, sending output to MessageBox.
    2.     Dim item1 As ListViewItem = findListView.FindItemWithText("brack")
    3.     If (item1 IsNot Nothing) Then
    4.         MessageBox.Show("Calling FindItemWithText passing 'brack': " _
    5.             & item1.ToString())
    6.     Else
    7.         MessageBox.Show("Calling FindItemWithText passing 'brack': null")
    8.     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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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

  6. #6
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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.
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  7. #7

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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

  8. #8
    Frenzied Member obi1kenobi's Avatar
    Join Date
    Aug 2007
    Posts
    1,091

    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?
    Please rate helpful ppl's posts. It's the best 'thank you' you can give

  9. #9

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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.
    Last edited by toecutter; Jan 7th, 2009 at 04:12 PM.

  10. #10

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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:
    1. ' Call FindItemWithText, sending output to MessageBox.
    2.     Dim item1 As ListViewItem = findListView.FindItemWithText("brack")
    3.     If (item1 IsNot Nothing) Then
    4.         MessageBox.Show("Calling FindItemWithText passing 'brack': " _
    5.             & item1.ToString())
    6.     Else
    7.         MessageBox.Show("Calling FindItemWithText passing 'brack': null")
    8.     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

  11. #11
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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:
    1. For i As Integer = 0 To Me.ListView1.Items.Count - 1
    2.             Dim item1 As ListViewItem = Me.ListView1.FindItemWithText(Me.TextBox1.Text, True, i)
    3.             If (item1 IsNot Nothing) Then
    4.                 item1.ForeColor = Color.Blue
    5.             End If
    6.             If (item1.SubItems IsNot Nothing) Then
    7.                 item1.SubItems(1).ForeColor = Color.Blue
    8.             End If
    9.         Next

    You can change the color per subitem if you wish.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  12. #12

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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

  13. #13
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Change listview Items Forecolor

    Could you try something like this also?

    VB.Net Code:
    1. Private Sub button1_Click_1(ByVal sender As Object, ByVal e As EventArgs)
    2.     Dim a As Integer = 0
    3.     Dim li As ListViewItem = listView1.FindItemWithText(Me.TextBox1.Text, True, a)
    4.     While (li IsNot Nothing) AndAlso (a < listView1.Items.Count - 1)
    5.         a = li.Index + 1 'start searching after the index of the list item found
    6.         li.ForeColor = Color.Blue
    7.         li = listView1.FindItemWithText(Me.TextBox1.Text, True, a)
    8.     End While
    9. End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  14. #14
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  15. #15

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    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

  16. #16
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  17. #17
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: Change listview Items Forecolor

    Ok pal, have a look at this, have fun!

    Vb.Net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim a As Integer = 0
    3.         Dim li As ListViewItem = lvwListView1.FindItemWithText(Me.TextBox1.Text, True, a)
    4.  
    5.         'continue searching while there is a match
    6.         While (li IsNot Nothing)
    7.             a = li.Index + 1 'start searching after the index of the list item found
    8.             li.UseItemStyleForSubItems = False
    9.  
    10.             ColorItems(li, Me.TextBox1.Text) 'color the column where the string was found
    11.            
    12.             If (a = lvwListView1.Items.Count) Then
    13.                 'we have reach the last item, exit now
    14.                 Exit While
    15.             End If
    16.  
    17.             li = lvwListView1.FindItemWithText(Me.TextBox1.Text, True, a)
    18.         End While
    19.     End Sub
    20.  
    21.     Private Sub ColorItems(ByVal li As ListViewItem, ByVal text As String)
    22.         If (li.SubItems.Count > 0) Then
    23.             Dim a As Int32 = 0
    24.             For a = 0 To li.SubItems.Count - 1
    25.                 If (li.SubItems(a).Text.IndexOf(text)) = 0 Then
    26.                     li.SubItems(a).ForeColor = Color.Blue
    27.                 End If
    28.             Next
    29.         End If
    30.     End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  18. #18

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Change listview Items Forecolor

    Thats perfect, thank you very much

  19. #19
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: [RESOLVED] Change listview Items Forecolor

    Glad to be of help.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

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