开发者

Accessing form's resources (resx file) from code

开发者 https://www.devze.com 2022-12-27 03:19 出处:网络
If I have a form Frm1.c开发者_StackOverflow社区s that is using some icons, images or other resources, these resources get stored in the form\'s resx file (Frm1.resx).

If I have a form Frm1.c开发者_StackOverflow社区s that is using some icons, images or other resources, these resources get stored in the form's resx file (Frm1.resx).

My simple question is how do I access these resources from code?

As a workaround I can store these resources in the Project resource file and they will be available via Properties.Resources.resourcename. However, similar syntax does not work for the resources stored in the form's resource file.

While searching for a solution I have come across several references to ResourceManager class but was not able to find a way to use that to access the form's resources...


The way to access local form resources is through an instance of ResourceManager. Suppose you got two PictureBox in a Form called Frm1:

var resources = new ResourceManager(typeof(Frm1));
var image = (Bitmap)resources.GetObject("pictureBox1.Image");

pictureBox2.Image = image;

Hope this could help you...


If you use visual studio designer to add resources, you get a class Resources with static properties to access them.

To access:

this.pictureBox1.Image = Properties.Resources.MyResourceImage;
0

精彩评论

暂无评论...
验证码 换一张
取 消