问题描述
我有一个名为"ExplanetoryForm"的表格.加载后,它将在图片框中显示图像(其路径位于资源文件中).
在我的主要形式(令人惊讶地称为"MainForm" :)中),我有一堆Linklabel,每个标签执行以下操作:
1.显示"ExplanetoryForm".
2.告诉"表单以加载图像,具体取决于其名称.
目前,我正在使用许多如果"语句来确定哪个资源适合哪个链接标签.
我想知道是否有办法替换我目前拥有的许多"if"句子(我有大约30个链接标签,结果是30个"if"句子").
过去,有人告诉我可以使用反射来完成此操作,但我想不出一种方法.
这是我的代码的一部分("if"语句列表).
Hi,
I have a form called "ExplanetoryForm". Upon loading, it displays an image (which its path located in the resource file) in a picturebox.
In my main form (surprisingly called "MainForm" :)) I have a bunch of Linklabels, each does the following:
1. Shows "ExplanetoryForm".
2. "Tells" the form to load an image, depending on its name.
Currently I''m using many "if" sentences to decide which resource fits which linklabel.
I''d like to know if there is a way to replace the many "if" sentences I currently have (I''ve got about 30 linked label, and as a result 30 "if" sentences").
In the past I''ve been told I could accomplish that by using reflection, but I can''t think of a way to do so.
Here is a part of my code (the "if" sentences list).
if (linkLabelName == "InstallerProp")
{
pictureBox1.Image = Image.FromFile(Resources.SpecLinksImages.InstallerFileProperties);
label1.Text = Resources.SpecLinksText.InstallerFileProperties;
}
if (linkLabelName == "InstallEnter")
{
pictureBox1.Image = Image.FromFile(Resources.SpecLinksImages.InstallUsingEnter);
label1.Text = Resources.SpecLinksText.InstallUsingEnter;
}
等...
谢谢
etc...
Thanks
推荐答案
System.Resources.ResourceManager rm = Resources.SpecLinksText.ResourceManager;
Bitmap myImage = (Bitmap)rm.GetObject(linkLabelName);
pictureBox1.Image = myImage;
希望对您有所帮助.
Hope it helps.
这篇关于根据发件人名称更改资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!