Question Can't change a button image using me.controls for some reason.

Runescope

Well-known member
Joined
Jan 6, 2011
Messages
53
Programming Experience
Beginner
So I have a series of buttons that I want the image to change when I click on them.

Here's the code I have so far:
VB.NET:
Private Sub btnFilter_Click(sender As System.Object, e As System.EventArgs) Handles btnMainFiltIn.Click, btnHistFiltIn.Click
        Dim BtnName, BtnNameF, BtnNameE As String
        BtnName = sender.Name : BtnNameF = Mid$(BtnName, 1, 7) : BtnNameE = Mid$(BtnName, 8)
        Select Case BtnNameE
            Case "FiltIn"
                btnMainFiltIn.BackgroundImage = iFilters.Images(1)
                Me.Controls(BtnName).BackgroundImage = iFilters.Images(1)
            Case "FiltOut"

            Case "FiltOff"

        End Select
End Sub

now, the line
VB.NET:
btnMainFiltIn.BackgroundImage = iFilters.Images(1)
works fine and does what I want, but I want to be able change the images programmatically based on the button clicked. Eventually there will be 12 buttons all linking to this code and it SHOULD be able to sort it out.

For some reason the line
VB.NET:
Me.Controls(BtnName).BackgroundImage = iFilters.Images(1)
crashes with a "Object reference not set to an instance of an object." error. (BtnName = 'btnMainFiltIn'. I checked :D )

Now what's REALLY strange is that in another part of the program I have this line.:
VB.NET:
Me.Controls(BtnName).BackgroundImage = iCalendar.Images(3)

And it works perfectly.

Soooooooooooo I'm confused. I can't figure out why one works and the other doesn't

Anyone have any ideas?

(Edit: Just a small rant. The "Object reference not set to an instance of an object." error is the most useless and generic error I have ever come across) [/rant]
 
Last edited:
Just a small rant. The "Object reference not set to an instance of an object." error is the most useless and generic error I have ever come across

No, it isn't. You just don't know what to do about it. That error message means that you are trying to access a member of an object that doesn't exist. As soon as you see that error message, the first thing to do is to check which reference is Nothing on that line. That takes seconds. Once you know which reference is Nothing, then you can work backwards to where you thought a value should be assigned and determine why it wasn't.

Anyway, why are you using the Name of the sender to get a reference to the Button when the sender already is a reference to the Button? Just do this:
DirectCast(sender, Button).BackgroundImage = iFilters.Images(1)
If you still get a NullReferenceException with that code then the only possibility is that iFilters is Nothing.

That said, I suspect that iFilters is an ImageList but I would recommend not using one of those unless you have to, e.g. you're using it with a ListView or TreeView. In your case, I would probably add the images as resources and then access them something like this:
Private someImage As Image = My.Resources.SomeImage

...

    DirectCast(sender, Button).BackgroundImage = Me.someImage
 
Well, first of all, thank you for responding with some useful suggestions.

However, if you had looked at the line ABOVE it, you would have noticed that iFilters works just fine. So that's not the problem. And if you'd read the rest of it, you would have seen that it's not a listview or treeview it's a series of buttons.

I also DO know what that error message means, and what I was saying is that it could at least tell me WHICH part of the line is null, if any. Because I've tested EVERY part of it, and there are no null values.

Now I would like to thank your for being completely condescending with your attitude towards someone why may not have your extensive knowledge. You have no idea of my experience, what kind of programming I've done or what kinds of courses I've taken (if any). Your implication that because I'm not using DirectCast I must be some kind of blithering idiot is highly offensive and not helpful or conducive to an environment of shared learning.

That being said, I will take your suggestions, and if they turn out to be useful I will use them and adopt them into my programming experience. Just because you're a complete prick, doesn't mean you can't be right.
 
Last edited:
Back
Top