RESOLVED - is string a valid color ?
im stuck can anyone help me out there has to be a better way to determin if a string is or is not a colour ....
this is what i've found but im not using it ....
Code:
Dim mycc As New ColorConverter
Try
Dim lbl As New Label
lbl.BackColor = mycc.ConvertFrom($STRING)
IsColour = True
Catch ex As Exception
IsColour = False
Return True
End Try
Re: is string a valid color ?
vb.net Code:
Function IsColor(ByVal TextName As String) As Boolean
If [Enum].GetNames(GetType(KnownColor)).Contains(TextName) Then Return True Else Return False
End Function
Re: is string a valid color ?
wow thanks that's awesome ! :)
going to test it now :D
Re: is string a valid color ?
if the string format is in RGB will this still work ?
Re: is string a valid color ?
No, this was meant for
Code:
If IsColor("AliceBlue") Then ...
Re: is string a valid color ?
Quote:
Originally Posted by
cicatrix
No, this was meant for IsColor("AliceBlue") Then ...
is there an easy way to check rather than breaking the string down ?
Re: is string a valid color ?
I'm afraid, no.
vb.net Code:
Function ColorFromRGBString(ByVal RGBString As String) As Color
' Input argument RGBString must be in "R, G, B" format,
' for example "215, 188, 212"
Dim rgbelems() As String = RGBString.Split(New Char() {","c})
If rgbelems.Count <> 3 Then
Throw New ArgumentException("Wrong argument " + RGBString)
Return Nothing
End If
Dim components(2) As Byte, x As Integer
For x = 0 To 2
If Not Byte.TryParse(rgbelems(x), components(x)) Then
Throw New ArgumentException("Wrong argument " + RGBString)
Return Nothing
End If
Next
Dim c As Color = Color.FromArgb(255, components(0), components(1), components(2))
Return c
End Function
Re: is string a valid color ?
thanks cicatrix these are both very useful ;)
i think i found an easy way ...
the string i have is either a file path or rgb value
since ","'s are not allowed in file paths im using the following check ...
if instr($string,",") > 0 then iscolour = true
but will put the above functions in my code bank
thanks
again
John
Re: is string a valid color ?
Your methods can lead to errors since you don't check what's in it.
For example, if I pass "A, B, C, D" to it it will return IsColor = True
Besides, you can create directories and filenames containing commas.
Check it yourself, in the command prompt type:
Code:
MkDir "C:\A,B,C"
echo test > "A,B,C.txt"
Re: is string a valid color ?
your right i just checked ... i thought ","'s were not allowed in file names ... but they are
.... the second function you posted is excellent im currently looking @ it now :D
Re: is string a valid color ?
Code:
Function ColorFromRGBString(ByVal RGBString As String) As Object
' Input argument RGBString must be in "R, G, B" format,
' for example "215, 188, 212"
Dim rgbelems() As String = RGBString.Split(New Char() {","c})
If rgbelems.GetLength(0) <> 3 Then
'Throw New ArgumentException("Wrong argument " + RGBString)
Return Nothing
End If
Dim components(2) As Byte, x As Integer
For x = 0 To 2
If Not Byte.TryParse(rgbelems(x), components(x)) Then
'Throw New ArgumentException("Wrong argument " + RGBString)
Return Nothing
End If
Next
Dim c As Color = Color.FromArgb(255, components(0), components(1), components(2))
Return c
End Function
i edited the code above ... the .count function wouldnt work on rgbemlems
so i replaced it with .GetLength(0) <> 3
is this correct ?
took out the argument exceptions and changed the return type to object
now it returns a color object if the string is a color and nothing if its not.
Code:
Dim c
c = ColorFromRGBString($string)
If IsNothing(c) Then
Exit Function
End If
Re: is string a valid color ?
As long as it works... :D
Re: is string a valid color ?
Quote:
Originally Posted by
cicatrix
As long as it works... :D
yes it seems to be working now :check:
thanks for your help :D:thumb: