ned223
ned223

Reputation: 13

How to change the image displayed on a button at runtime in Visual Basic?

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.

Upvotes: 1

Views: 2151

Answers (3)

ned223
ned223

Reputation: 13

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

Upvotes: 0

jmcilhinney
jmcilhinney

Reputation: 54417

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.

Upvotes: 2

JayV
JayV

Reputation: 3271

(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")
    

    Upvotes: 0

    Related Questions