Referencing Dynamic Control Array Data

AtadPO

New member
Joined
Apr 16, 2019
Messages
4
Programming Experience
10+
I'm working on a small project and trying to familiarize myself with C# to boot (perhaps in a ham-fisted way).

The program dynamically creates an array of buttons and uses an imagelist to place an image on the button (not the button background). Clicking on the button increments which image is displayed on the buttons.

The issue I have is that I wish to save in a text file the current image index of the image - so it can be reloaded in future. Whilst I can reference and change certain things like text and background image - I cannot work out a way to access the index of the image currently in-use.

Appreciate I've provided only a little info, if more it required I will do my best to answer.
 
Okay, so there is also a toolstripmenu (created in the Designer) with a save option added. When the user clicks save, I cannot access the ".ImageIndex" of the button.

I'm attempting to access each button in a loop like this:

this.tabControl_Schedules.TabPages[tabpage_counter].Controls["button" + announcement_counter++].Text

In this example, I'm able to access the text of the button.
 
That's because, when you index the Controls property, you get a Control reference back and the Control class has no ImageIndex property. That property is a member of ButtonBase so, if you are referring to controls that have that property, you need to cast them as an appropriate type to be able to access properties of that type. If you're referring only to Button controls then it makes sense to cast as type Button.

Hopefully this demonstrates why you should ALWAYS post your code when asking about an issue. If what you're doing isn't working then you are almost certainly doing it wrong, but we can't tell you what's wrong with it if we can't see it.
 
Back
Top Bottom