[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
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:
Dim lvi As ListViewItem = New ListViewItem
lvi.Text = "Test"
lvi.BackColor = Color.Blue
ListView1.Items.Add(lvi)
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.
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.
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:
Dim tmpColor As New System.Drawing.Color
tmpColor = Color.Blue
ListView1.Items(0).BackColor = tmpColor
Re: Set listview item's background color from a string
Quote:
Originally Posted by
weirddemon
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:
Dim tmpColor As New System.Drawing.Color
tmpColor = Color.Blue
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.
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?
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)
Re: Set listview item's background color from a string
Quote:
Originally Posted by
NickThissen
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.