Results 1 to 9 of 9

Thread: [RESOLVED] Set listview item's background color from a string

  1. #1

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Resolved [RESOLVED] Set listview item's background color from a string

    I'm kind of stuck on this one, and i know it's going to be something simple, but I just can't come up with the solution. I want to be able to assign a background color for each item in a listview with a predefined string or other variable type, so when item's are added to the listview and they contain a specific string, the background color for that item will be changed to the predefined color which is stored in a string (or other variable). When I attempt to change the background color of the listview item using a string ie. "Blue" I get an error. Any help would be appreciated.


    In a nutshell, here is what I am attempting to do but get errors:
    Code:
    Dim tmpClor as string 
    tmpColor = "Blue" '//I want to be able to store the color in a string or some other variable
    LV1.Items.Item(1).BackColor = tmpColor '//Assign the back color of the item in LV1 via the string
    Last edited by SavedByGrace; Dec 21st, 2009 at 06:32 PM. Reason: Spelling corrections

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Set listview item's background color from a string

    You'll probably need to assign the .BackColor of the ListViewItem before you add it to the control.

    ie:

    vb Code:
    1. Dim lvi As ListViewItem = New ListViewItem
    2.         lvi.Text = "Test"
    3.         lvi.BackColor = Color.Blue
    4.         ListView1.Items.Add(lvi)
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Re: Set listview item's background color from a string

    That won't work with what I am attempting to do, the background colors will need to be applied on the fly. Thanks though.

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Set listview item's background color from a string

    You'll need to explain your situation then. We'll also need to know how you're adding ListViewItems to the control.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Set listview item's background color from a string

    Also, System Colors aren't a string. They're a ReadOnly Property. They're part of System.Drawing and you have to treat them as such.

    So, with that being said, you can do what you want with your code if you assign the variable appropriately:

    vb Code:
    1. Dim tmpColor As New System.Drawing.Color
    2. tmpColor = Color.Blue
    3.  
    4. ListView1.Items(0).BackColor = tmpColor
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Re: Set listview item's background color from a string

    Quote Originally Posted by weirddemon View Post
    Also, System Colors aren't a string. They're a ReadOnly Property. They're part of System.Drawing and you have to treat them as such.

    So, with that being said, you can do what you want with your code if you assign the variable appropriately:

    vb Code:
    1. Dim tmpColor As New System.Drawing.Color
    2. tmpColor = Color.Blue
    3.  
    4. ListView1.Items(0).BackColor = tmpColor

    I understand that colors are not a string and are read only, but for my app, they must be assigned by a string/text stored in a file which is there for later reference/use. Let me attempt to explain it a little more clearly:

    1. I decide I want items in the listview with a predefined text to have a certain background.
    2. While the app is running, i change the background color to that color, and I save that string/color assignment to a .dat file for the next time i run the app so that I do not have to manually re-assign item's with the key text the background color again. Also, I do not want to have to add new code to the app every time I decide a specific item should have a different background color.
    3. When I run the app again, it will load those settings and then load the items going into the listview and assign them the appropriate background color.

  7. #7
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,828

    Re: Set listview item's background color from a string

    Regardless of what your app requires, I still don't believe you can use a string. I might be wrong, but I don't think so. Not in this case, anyway. You can do as I suggested. The problem now is that you need to know how to save the information to a file. For that, I believe you need to create a class and make applicable properties.

    How are you saving the information at the moment?
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

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

    Re: Set listview item's background color from a string

    You can certainly get a color from it's name; check out the Color.FromName function. I think the names you can pass it are the same names as you can find in the Color class (like Color.Blue would be "Blue", Color.Red would be "Red" and Color.Lightblue would be "Lightblue". Also I don't think capitalization matters.)
    EDIT
    Here's the full list of color names you can use.

    In your situation:
    Code:
    Dim tmpColor As Color
    tmpColor = Color.FromName("Blue")
    LV1.Items.Item(1).BackColor = tmpColor
    or equivalently
    Code:
    Dim tmpColor As String
    tmpColor = "Blue"
    LV1.Items.Item(1).BackColor = Color.FromName(tmpColor)

  9. #9

    Thread Starter
    Addicted Member SavedByGrace's Avatar
    Join Date
    May 2006
    Posts
    130

    Re: Set listview item's background color from a string

    Quote Originally Posted by NickThissen View Post
    You can certainly get a color from it's name; check out the Color.FromName function. I think the names you can pass it are the same names as you can find in the Color class (like Color.Blue would be "Blue", Color.Red would be "Red" and Color.Lightblue would be "Lightblue". Also I don't think capitalization matters.)
    EDIT
    Here's the full list of color names you can use.

    In your situation:
    Code:
    Dim tmpColor As Color
    tmpColor = Color.FromName("Blue")
    LV1.Items.Item(1).BackColor = tmpColor
    or equivalently
    Code:
    Dim tmpColor As String
    tmpColor = "Blue"
    LV1.Items.Item(1).BackColor = Color.FromName(tmpColor)

    Perfect, exactly what I was looking for. Thank you for this solution NickThissen. Rep given.

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