1

We want to dynamically (depending on a XML File) set the BackColor of a Control in Windows Forms.

I just tried the CallByName-Method with CallType.Method.

CallByName(ColorObject, "FromName", CallType.Method, "Red")

When I want to execute the "FromName" Method it doesn't work, because I need it like this "Color.FromName".

The CallByName got a object from the Class "Color" but still doesn't work. The "Color"-Class is imported.

Does anybody got an idea what I'm doing wrong or how I could solve this problem?

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Dyrdek
  • 411
  • 4
  • 12
  • 33

1 Answers1

0

It's not clear whether the control name and the colour are in the Xml so I will cover both.

What you want is Controls.Find and Color.FromName

So combining you can do something like this:

Dim ctl = Me.Controls.Find("Controlname from Xml", True) 'omit the True if you don't need to search child controls as it will be faster
Dim myColour = Color.FromName("ColourName From Xml")
ctl.backColor = myColour

Note that the colour name must be one of the standard .Net colours in the KnownColor Enum otherwise it will throw an exception

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
  • Thanks :) But we need to dynamically choose the method. We solved this problem with "CallByName" so we can extract a String from a XML File and call the suitable method. The problem with with "FromName" is, that we need the "Color" in front of it (Color.FromName). And with CallByName we aren't able to export "Color.FromName" as a String. That way CallByName doesn't find any method. – Dyrdek Oct 02 '15 at 12:39