-1

I've defined a new System.Drawing.Color;

Public CustomBackColor As System.Drawing.Color = Color.FromArgb(200, 4, 146)

Now, I have a GlobalBackColor As String which I wanted to become my new CustomBackColor Value.

My Question is, if there is any way to write CustomBackColor System.Drawing.Color, to the GlobalBackColor String. Or, to make the CustomBackColor a default System Color, so that I could define the GlobalBackColor like so: GlobalBackColor = "CustomBackColor"

Anyways, I would like to be able to define it like this: pbR001C03.BackColor = Color.FromName(GlobalBackColor)

EDIT:

pbR001C03 is a PictureBox.

In this example Example;

Public CustomBackColor As System.Drawing.Color = Color.FromArgb(200, 4, 146)

Public GlobalBackColor As String

Public Sub Example()

`GlobalBackColor = Color.FromName(CustomBackColor)`
`...`
`pbR001C03.BackColor = Color.FromName(GlobalBackColor)`
`pbR001C04.BackColor = Color.FromName(GlobalBackColor)`
`...`

End Sub

the GlobalBackColor = Color.FromName(CustomBackColor) does not seem to work for me. No error, just a black Color.

WashirePie
  • 75
  • 9
  • 1
    it `pbR001C03.BackColor = Color.FromName(GlobalBackColor)` works fine for me, when `pbR001C03` is a form control. here in your case what is `pbR001C03`? and what was the error you are getting? –  May 21 '15 at 07:23
  • I've added an example to my question ^ – WashirePie May 21 '15 at 07:50

2 Answers2

1

You can't use Color.FromName like this, but you don't need to use it.
Since your CustomBackColor is of type System.Drawing.Color you can use it directly to any property of type System.Drawing.Color

pbR001C03.BackColor = GlobalBackColor

here is the method description From MSDN:

Creates a Color structure from the specified name of a predefined color.

(not bold in source).

There really is no possible way to use Color.FromName with custom names, since it would require to extend the System.Drawing.KnownColors enumeration, and extending enums is impossible in .net

Community
  • 1
  • 1
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Yep, I that's true. Problem is that I need `GlobalBackColor` to be a `String`. – WashirePie May 21 '15 at 08:05
  • Why do you think you need it as a string? – Zohar Peled May 21 '15 at 08:15
  • Because `GlobalBackColor` is used for multiple Actions in the app source code I'm editing at the moment. Otherwise I would've obviously used the `Color`Type for `GlobalBackColor`;) – WashirePie May 21 '15 at 08:23
  • So use the color directly, Why does it have to be a string? can you provide a concrete example of a case where you can use the string but can't use the color? – Zohar Peled May 21 '15 at 08:40
  • f.e. `Public Sub SolidFill(Optional BackColor As String = "Black")` `...` `End Sub` If I'd use a `Color` Type for BackColor, it'd throw a Constant Expression required exception. – WashirePie May 21 '15 at 08:57
  • If you have methods like this then your design is simply **wrong**. Change these methods to get a `System.Drawing.Color` instead of a `string. – Zohar Peled May 21 '15 at 09:15
  • I'm sorry, I am just editing this code, it's not my creation. Thanks for the Tips though. – WashirePie May 21 '15 at 09:23
  • nothing to be sorry about, even if it was your own code, everybody is allowed to make mistakes. There really is no possible way to use Color.FromName with custom names, since it would require to extend the System.Drawing.KnownColors enum, and extending enums is impossible in .net – Zohar Peled May 21 '15 at 09:30
  • Aha, this is what I was trying to find out. Could you please Post this as an Answer? Thanks! – WashirePie May 21 '15 at 09:34
  • I've edited my answer to include my previous comment as you requested. – Zohar Peled May 21 '15 at 10:15
0

Afaik changing/extending the System.Drawing enumeration is not possible in vb.net

WashirePie
  • 75
  • 9