2

I want to set an image as a background to my button in datagridview. So I follow the following answer DataGridView Image for Button Column. I have added my image to the resources like this :

enter image description here

My problem is I can't access the image in the resources.

Properties.Resources. //Here the intellisense doesn't give me the name of my resource.

How to access my resource?

Anyname Donotcare
  • 11,113
  • 66
  • 219
  • 392
  • directly resource name `StatementPrint.printer` – Abdullah Aug 03 '19 at 19:37
  • 1
    You have added the image to local resource of the form. Add it to project resource file. Also keep in mind, local resource of form doesn't need code generation, so instead of `Publlic` which you have chosen for `Access Modifier` in resource designer, choose `no code Generation`. But for the project Resource file let it be `internal` (which is by default.) – Reza Aghaei Aug 03 '19 at 21:31
  • 1
    @RezaAghaei thanks a lot, this was the problem, because i use it as local resource – Anyname Donotcare Aug 03 '19 at 22:59

1 Answers1

2

Add an image to your project resource file

  1. Go to Solution Explorer → Project node → Properties folder → Resources.Resx
  2. Open Resources.Resx in design view by double click on the file.
  3. Then it's enough to drag an image from windows explorer and drop it into the designer. The image will be added to the Resources.Resx.
  4. As another option, you can click on Add Resource dropdown and choose Add Existing File... from menu.

    enter image description here

For example if you have added MyImage.png to resource file the you can access it in code this way:

this.BackgroundImage = Properties.Resources.MyImage;

Just in case your project doesn't have such Resources.Resx file

  1. Right click on Project → Choose Properties
  2. In project properties window, choose Resources (left side, bottom of the list).
  3. At the center, you will see a link This project does not contain a default resources file. Click here to create one. Click on the link and it will create a Resources.Resx file under Properties folder for your project.

Select Resource Dialog for Image properties

You can also add the image to project resource file from Select Resource dialog. To do so, open your Form in design view:

  1. Open Properties window → BackgroundImage property → Click on ... button in front of the property in property grid.

  2. In the Select Resource dialog, make Properties\Resources.Resx has been selected in combobox.

  3. Click on Import button and choose an image and press OK.

The designer will add the image to resource file and set it as BackgroudImage property of the form.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398