1

I am trying to change the Image attribute associated with a button when the button is clicked. I have added the image I want to display (called "Black Pawn.png") as a resource within the solution and (from searching around and looking at similar questions) am trying to set the image attribute to it as shown below:

Private Sub boardButtonA2_Click(sender As Object, e As EventArgs) Handles boardButtonA2.Click
        Dim sprites As Object = My.Resources.ResourceManager
        boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
    End Sub

But when I click the button, the default image on it just disappears and is replaced with nothing instead of Black Pawn.png.

I am pretty new to using Visual Basic and Visual Studio so I may have failed to have added the image as a resource properly but I can see the image in the Solution Explorer above the form the button is in.

Any help or advice would be a great help. Thanks.

1
  • Can you add a tag for the technology, please? WinForms, WPF, Web/ASP.Net Commented Feb 13, 2021 at 22:36

3 Answers 3

2

EDIT:

Having thought more about the potential scenario, I'm wondering whether this answer is actually relevant. Your example code uses a hard-coded String but I'm wondering whether the actual String really would be a user selection. I'll leave it here anyway.

ORIGINAL:

There's no reason to use a hard-coded String to get a resource. If the String was from user entry then maybe, depending on the circumstances. As it is though, you should be using the dedicated property for that resource. That means using this:

boardButtonA2.Image = My.Resources.Black_Pawn

Just be aware that a new object is created every time you get a resource from My.Resources. For that reason, don't keep getting the same property over and over. If you need to use a resource multiple times, get it once and assign it to a field, then use that field multiple times, e.g.

Private blackPawnImage As Image

Private Function GetBlackPawnImage() As Image
    If blackPawnImage Is Nothing Then
        blackPawnImage = My.Resources.Black_Pawn
    End If

    Return blackPawnImage
End Function

and then:

boardButtonA2.Image = GetBlackPawnImage()

Also, I suggest that you change the name of the property to BlackPawn rather than Black_Pawn. You can change it to whatever you want on the Resources page of the project properties.

EDIT:

If this application is a chess game then you definitely will need every resource image for the playing pieces so you probably ought to get all of them at load and assign them to variables, then use them from those variables over the course of the game.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, you've helped me understand how resources work much better now! I think my issue was that I didn't properly add the image as a resource, but the method of assigning all the images when the form is loaded as you mentioned is better than what I was thinking considering that it would create a new object every time.
0

(I am assuming the question is for WinForms, if it isn't I will remove this answer)

When adding images as resources to a project, you have to pay attention to the name given to the resource after it is imported - the name can be changed if you want.

The image below is from one of my projects:

enter image description here

The name of the files on disk are:

  • CSV - Excel.png
  • GreenDot.png
  • RedDot.png

  • To fix your problem change the line:

    boardButtonA2.Image = sprites.GetObject("Black Pawn.png")

    to be:

    boardButtonA2.Image = sprites.GetObject("Black_Pawn")
    

    1 Comment

    I'll keep that in mind, thank you! Your screenshot helped me realise the way I was adding the image as a resource was just totally wrong. If anyone else has a similar issue to me then see my answer below for how I fixed it.
    0

    I don't think adding the image by going into Project > Add Existing Item properly added the image as a resource.

    I instead went into *Project > (Solution Name) Properties > Resources > Images > Add Existing Item * and added it that way and was then able to get it working using jmcilhinney's method (I think my original method/a variation of it would work too but theirs is better).

    Comments

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.