references in visual c#
I would like to select an image reference from a string. This looks quite simple but I am new to c#.
My code at the moment looks like:
switch (stringCharacterName1)
{
case "Paul": picCharacter1.Image = WindowsApplication.Properties.Resources.Paul; break;
case "Bob": picCharacter1.Image = WindowsApplication.Properties.Resources.Bob; break;
and so on...
}
switch (stringCharacterName2)
{
case "Paul": picCharacter2.Image = WindowsApplication.Properties.Resources.Paul; break;
case "Bob": picCharacter2.Image = WindowsApplication.Properties.Resources.Bob; break;
and so on...
}
and so on...
I have 32 names and 8 character selection so it is quite long...
I am searchi开发者_运维百科ng for a way to write this like:
picCharacter1.Image = WindowsApplication.Properties.Resources.stringCharacterName1;
picCharacter2.Image = WindowsApplication.Properties.Resources.stringCharacterName2;
But it doesn't work... Maybe this problem has already been solved but I don't find something similar. Thanks for help
Try with the following code :
picCharacter1.Image = (Image)Resources.ResourceManager.GetObject(stringCharacterName1);
精彩评论