visual c中的引用

时间:2011-09-19 08:23:14

标签: c#

我想从字符串中选择一个图像引用。这看起来很简单,但我是c#的新手。

我的代码目前看起来像:

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...

我有32个名字和8个字符选择所以它很长......

我正在寻找一种方法来写这个:

picCharacter1.Image = WindowsApplication.Properties.Resources.stringCharacterName1;
picCharacter2.Image = WindowsApplication.Properties.Resources.stringCharacterName2;

但它不起作用......也许这个问题已经解决但我找不到类似的东西。谢谢你的帮助

1 个答案:

答案 0 :(得分:5)

尝试使用以下代码:

picCharacter1.Image = (Image)Resources.ResourceManager.GetObject(stringCharacterName1);