I'm not sure how to describe this in the title, so it may be misleading.

Basically, I want to change the image of a LOT of buttons. And when I say a lot, I mean 93 of them.

So obviously, this approach:

btnSinglePlay.BackgroundImage = Image.FromFile(buttonN);

would take too long, since I have to repeat it for every button, and could be prone to errors.

Researching on the internet gave me this code:

this.pnlBoard.Controls[buttonName].BackgroundImage = Image.FromFile(pieceName);

where I believe I can simply change the (string) buttonName via a for loop, saving me from doing it 93 times EVERY time I need the buttons to change images.

Notice that this code only allows me to change BACKGROUND image, and not IMAGE, for reasons I don't know.

However, executing that code gets me the following error:

NullReferenceException was unhandled.
Object reference not set to instance of an object.

I have no idea what this means. Presumably there has to be a " = new Control() " somewhere in there, but I don't know where / how.

Any ideas on this?

Also, is there any other similar way / code that would allow me to change the images of a button, without have to go through them one by one?

Thanks in advanced.

Recommended Answers

All 7 Replies

What it means is that the name you are providing inside the [] doesn't exist, so it's a null and you can't set the BackgroundImage of a null.

I'd also load the image once, then set all the buttons to that image so you don't have to do 92 disk accesses to load it from a file.

Thanks for the reply.

The buttonName? I instantiated it above. Maybe I should've posted the entire code, sorry.

string buttonName = "";
            string pieceName = "";
            int iPlus = 0;
            if (pieceColor == "b")
            {
                for (int i = 0; i < 21; i++)
                {
                    iPlus = i + 1;
                    buttonName = "P" + iPlus.ToString();
                    if (i < 10)
                    {
                        pieceName = "App_Pics/piece_" + "0" + iPlus.ToString() + "_b.png";
                        this.pnlBoard.Controls[buttonName].BackgroundImage = Image.FromFile(pieceName);
                    }
                    else
                    {
                        pieceName = "App_Pics/piece_" + iPlus.ToString() + "_b.png";
                        this.pnlBoard.Controls[buttonName].BackgroundImage = Image.FromFile(pieceName);
                    }
                }
            }

As for loading the image at once, how do you do that? The Images of the buttons change all the time.

Load the images into an Image array and then use that to set the buttons.

Where do you name your buttons? That's the important part :)

I created my buttons using the drag and drop, and renamed them via the properties panel, if that's what you mean.

My buttons are named with a letter and a number. Basically what I want to happen is that a string is created, a loop modifies the string and the number (say, A1) and the image of the button named A1 changes.

I checked that code snippet, although for some reason it won't work for me, regarding the first 'if'. I used a Panel instead of a Tab Page, but the rest of the code is similar.

As for Array of Buttons, it looks intriguing, but reading a bit more about it there seems to be some difficulty regarding click events. I'm not actually so sure about that one.

The code snippet you posted seems the best way to deal with this. I'll just figure out what I did wrong.

Thanks for the replies.

Well, after research, I came up with this. Seemed to solve my problems.

for (int i = 0; i < 21; i++)
                    {                     
                        pieceName = "App_Pics/piece_" + "0" + i.ToString() + "_b.png";
                        Control btn = this.Controls.Find(buttonName, true)[0] as Control;
                        btn.BackgroundImage = Image.FromFile(pieceName);
                    }

Thanks for the replies, guys.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.