• I removed the www from the URL, please update your bookmarks and pay attention to the bottom of the screen if using Google Chrome to allow push notifications again.

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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,230
Location
Sydney, Australia
Programming Experience
10+
If you can't determine the index of the current image then how are you changing which image is displayed when you click a Button?
 

AtadPO

New member
Joined
Apr 16, 2019
Messages
4
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,230
Location
Sydney, Australia
Programming Experience
10+
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.
 

AtadPO

New member
Joined
Apr 16, 2019
Messages
4
Programming Experience
10+
Thank you, that gives me something to work with. Learning process - thanks for your patience.
 

AtadPO

New member
Joined
Apr 16, 2019
Messages
4
Programming Experience
10+
Finally, thanks to you I see what was needed. I have some fundamentals to get acquainted with it seems :)
 
Top Bottom