Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing form's resources (resx file) from code

Tags:

c#

.net

winforms

If I have a form Frm1.cs 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...

like image 629
Dean Kuga Avatar asked Apr 15 '10 03:04

Dean Kuga


People also ask

How do I read a RESX file?

Create an App_GlobalResources system folder and add a resource file to it e.g. Messages. resx. Create your entries in the resource file e.g. ErrorMsg = This is an error. Then to access that entry: string errormsg = Resources.

How do I open resources in Resx?

resx file in the XML (Text) editor. Press Alt+\ or choose ReSharper | Navigate | Go to File Member… from the main menu . Alternatively, you can press Ctrl+Shift+A , start typing the command name in the popup, and then choose it there. You will see the list of resource entries in a popup.


1 Answers

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

like image 192
ClownCoder Avatar answered Sep 23 '22 07:09

ClownCoder