|
-
Feb 15th, 2010, 06:09 AM
#1
Thread Starter
Lively Member
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
Last edited by illskills; Feb 15th, 2010 at 08:01 AM.
-
Feb 15th, 2010, 06:30 AM
#2
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
-
Feb 15th, 2010, 06:58 AM
#3
Thread Starter
Lively Member
Re: is string a valid color ?
wow thanks that's awesome ! 
going to test it now
-
Feb 15th, 2010, 07:00 AM
#4
Thread Starter
Lively Member
Re: is string a valid color ?
if the string format is in RGB will this still work ?
-
Feb 15th, 2010, 07:01 AM
#5
Re: is string a valid color ?
No, this was meant for
Code:
If IsColor("AliceBlue") Then ...
-
Feb 15th, 2010, 07:02 AM
#6
Thread Starter
Lively Member
Re: is string a valid color ?
 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 ?
-
Feb 15th, 2010, 07:08 AM
#7
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
-
Feb 15th, 2010, 07:13 AM
#8
Thread Starter
Lively Member
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
-
Feb 15th, 2010, 07:21 AM
#9
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"
-
Feb 15th, 2010, 07:28 AM
#10
Thread Starter
Lively Member
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
-
Feb 15th, 2010, 07:49 AM
#11
Thread Starter
Lively Member
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
-
Feb 15th, 2010, 07:54 AM
#12
Re: is string a valid color ?
As long as it works...
-
Feb 15th, 2010, 08:03 AM
#13
Thread Starter
Lively Member
Re: is string a valid color ?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|