I'm trying to add an image to a ToolStrip. When trying to add the image, I get the error saying 'dog' is not a member of 'ToolStripEvents.My.Resources'.
I have added a new Resource File to my Visual Studio project and added the two existing images to the resource file. In the Solution Explorer, I see the Resources folder has the two images (dog.jpg and Euclid.jpg) in it.
In my Design for the form, I added the ToolStrip along with a Button. For the Button click event, I'm trying to add an image to the ToolStrip:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim atoolstripitem As ToolStripItem
atoolstripitem = myToolStrip.Items.Add(My.Resources.dog)
End Sub
End Class
Am I added the image resources correctly? If not, how can I add them correctly so I can use them in the ToolStrip?
CodePudding user response:
When you add a Resource file to the Project, via Project -> Add New Item -> Resource File
, you have to use the name you have assigned to this new Resource file, which is also assigned to the internal
(Friend
) class object that is created in the corresponding [Resource].Designer.xx
file.
These Resource files are usually created in the root of the Project's folder structure (unless otherwise specified).
Assume the new Resource file has been named ResourcesExtra
: you access its resource objects as ResourcesExtra.SomeResourceName
(ResourcesExtra.dog
here)
The OP is instead trying to access a resource image using My.Resources
.
My.Resources
points to the default Resource file associated with the Project, stored in the My Project
folder in a VB.Net Project, or the Properties
folder in a C# Project.
Of course, this Resource file doesn't hold a reference to the location of the Image stored in another Resource file, hence the exception.
Images added to Resource files are stored in the Project's Resources
folder (VB.Net and C# Projects). Other types of resources in different locations (e.g., \Obj\Debug
in case of files)
An extra Resource file is apparently not actually needed in this specific case, so moving the image to the Project's Resources is probably a good option (also removing the extra Resource file).
But using different Resource files for different destinations can be a good idea.
Also, you could extend the concept and move your Resources to a satellite assembly (a Library Project), used as storage for all the Resources need in a Project, adding static (Shared) methods to retrive these objects, similar to what the ResourceManager does, but in a custom (specialized) manner.
Resources can be retrieved by name, index etc. and organized as required, e.g., to handle multiple languages.