Hello everyone.
Im making a application in which i need to make a matrix of 8x8x8 buttons.
Each layer contains 8x8 buttons (so 8 layers in total).
Each button can be clicked and when clicked it change the color to red, the second click changes the color of the button to green, and the third click makes it blanc again.
I tried making different arrays for each row, but this didnt work out.
Can anyone help me creating this application?

Recommended Answers

All 3 Replies

Create all your buttons, you don't need them in an array (unless there is some specific reason you do). Create one handler event and use the sender parameter to get the button that was clicked. Set all the clicked events to this handler. Get the current color and figure out what the next color should be and set it to that.

the msdn library even has an article that deals specifically with buttons.

Okay I am actually about to leave but I have some old code that might help. NOTE THIS IS FOR PANELS NOT BUTTONS but the concept might give you some assistance

Control [] controls;

controls = new Control [] {null, panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8, panel9, panel10, panel11, panel12,
                                       panel13, panel14, panel15, panel16, panel17, panel18, panel19, panel20, panel21, panel22, panel23, panel24,
                                       panel25, panel26, panel27, panel28, panel29, panel30, panel31, panel32, panel33, panel34, panel35, panel36,
                                       panel37, panel38, panel39, panel40, panel41, panel42, panel43, panel44, panel45, panel46, panel47, panel48,
                                       panel49, panel50, panel51, panel52, panel53, panel54, panel55, panel56, panel57, 
                                       //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                       panel91, panel92, panel93, panel94, panel95, panel96, panel97, panel98, panel99, panel100, panel101, panel102, 
                                       panel103, panel104, panel105, //first bottowm row
                                       //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                       panel58, panel59, panel60, panel61, panel62, panel63, panel64, panel65, panel66, panel67, panel68, panel69, 
                                       panel70, panel71, panel72, panel73, panel74, panel75, 
                                       //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                       panel106, panel107, panel108, panel109, panel110, panel111, panel112, panel113, panel114, panel115, panel116, panel117, 
                                       panel118, panel119, panel120,  //second bottom row
                                       //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
                                       panel76, panel77, panel78, panel79, panel80, panel81, panel82, panel83, panel84, panel85, panel86, panel87, 
                                       panel88, panel89, panel90};

foreach (Control c in controls)
{
    if (c != null)
    {
        c.MouseEnter += new EventHandler(mouseEnterEvent);
        c.MouseLeave += new EventHandler(mouseLeaveEvent);
        c.Click += new EventHandler(mouseClickEvent);          
    }
}

ONCE AGAIN THIS WAS BUILT FOR PANELS NOT BUTTONS but I think it should help. Here I had placed the panels ahead of time so they already existed (it was for a periodic table of elements). I also assigned it event controlllers for when the mouse hovered over it (MouseEnter), when the mouse left (MouseLeave), and when I clicked a panel (Click).

Again I had created the panels ahead of time so they were already on the form.

You could use a similar concept, however you might want to check if you are using Control or something else. MSDN should help with that.

This worked nice though because I could access the panels using indexing (oh also the first index I did as null due to a personal preference you do not have to do that).

Sorry I couldn't give you a working example for you code, but I do hope this helps

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.