Reputation: 105
My problem is that I'm trying to parse a String to a System.Drawing.Color. Im trying to set up a simple notepad, here's part of my code:
Private Sub ToolStripMenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Colorfuente2.Click
Try
Dim cdlg As New ColorDialog
cdlg.ShowDialog()
cdlg.FullOpen = True
cdlg.AnyColor = True
ColorFuente1.Visible = True
Colorfuente2.Visible = False
If Windows.Forms.DialogResult.OK Then
RichTextBox1.ForeColor = cdlg.Color
reciente2.Text = cdlg.Color.ToString 'I've converted this tostring, so that recent colors are shown as text, this is what im trying to reverse
End If
Catch ex As Exception
End Try
End Sub
If Reciente1.Text = "Ninguno" Then
MessageBox.Show("No hay colores recientes", "Bloc de notas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else : RichTextBox1.ForeColor = Reciente1.Text 'I get the error here, I have to change this text to a System.Drawing.Color.
End If
Thanks in advance.
Upvotes: 2
Views: 4734
Reputation:
When you use cdlg.Color.ToString
it doesn't really convert it to a string that it can read afterwards. It just convert it to something like "color [Yellow]".
If you want to use Color.FromName
you will have to pass it just "Yellow", otherwise it will return something unexpected. Probably a color object with a default value o a nothing value.
I'd suggest you to use a ColorConverter
Dim colorConv As New ColorConverter
TextBox1.Text = colorConv.ConvertToString(cdg.Color)
This will return a string "Yellow", that you can use however you like.
'Using FormName
TextBox1.BackColor = Color.FromName(TextBox1.Text)
'Using the color converter again (recommended).
Dim colorConv As New ColorConverter
TextBox1.BackColor = colorConv.ConvertFromString(TextBox1.Text)
You can also use substring to get the "Yellow" part in "Color [Yellow]". :P
Upvotes: 5
Reputation: 499092
You need to get a Color object in order to assign it to ForeColor
.
The Color.FromName
method will take a string
and return a matching Color
object (assuming it exists):
If Reciente1.Text = "Ninguno" Then
MessageBox.Show("No hay colores recientes", "Bloc de notas", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Dim col As Color = Color.FromName(Reciente1.Text)
RichTextBox1.ForeColor = col
End If
Upvotes: 3
Reputation: 45083
So long as the name is proper, and I expect it would be if you're using ToString
(**) and not messing with it otherwise, yo ucan use the Color.FromName
method. Although you would have to be careful; if the reciente
content is editable at all you could be in for some trouble, so naturally, you should attempt the conversion back to work, but maybe not expect it to.
** It is possible for Color.ToString
to return something other than a definite name of the colour:
'A string that is the name of this Color, if the Color is created from a predefined color by using either the FromName method or the FromKnownColor method; otherwise, a string that consists of the ARGB component names and their values.'
Upvotes: 0
Reputation: 45145
Is this WinForms or WPF?
In WinForms, there's a Color.FromName that will convert a know color name back to a color. So you can do something like this:
Color.FromName("SlateBlue")
In WPF, I believe you can use the ColorConverter class.
Upvotes: 0