I
I
isxaker2014-03-25 13:23:58
.NET
isxaker, 2014-03-25 13:23:58

How to work with the user control resource file?

At the moment I am developing under winforms, writing custom controls. There was a question where to store pictures which this control uses. I decided that I would store them in the resource file (.resx) not of the entire application, but only of my control.
The question is, how do you pull pictures from the control/form resources? If you store pictures in a project resource file, then there are no questions:

System.Drawing.Image myImg = Properties.Resources.ImageName;
or
System.Drawing.Image myImg = global::ProjectName.Properties.Resources.ImageName;

However, if I use a control/form resource file, then I have to do this:
using System.ComponentModel;

//init
ComponentResourceManager resources = new ComponentResourceManager(typeof(MyControlName));
//get image
System.Drawing.Image myImg = resources.GetObject("MyImageName");

The problem is that you have to write the name of the image file with pens. Is it possible to work with a control/form resource file in the same way as with a project resource file, i.e. without lines?
My workaround is to create a static readonly field that holds the name of the image file:
private readonly static string myImgName = "myImg_16x16";

respectively, then I can use like this:
System.Drawing.Image myImg = resources.GetObject(myImgName);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dobriykot, 2014-08-21
@dobriykot

If I understand the problem correctly, this is easily solved. Open the resource file, the one with the default culture, and set access modifier = internal. This is the drop-down list at the top:
Most likely, you currently have No code generation.
Resources should be accessed like this:
The resource file name is the same as the class name. Those. I have Strings.resx and Strings.ru.resx.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question