简体   繁体   English

视觉C#中的引用

[英]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#. 这看起来很简单,但是我是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... 我有32个名字和8个字符的选择,所以很长。

I am searching 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 感谢帮助

尝试以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM