GavinP
GavinP

Reputation: 687

Identify mouseclick within a panel and it's associated controls c#

I have a panel which is sized according to the required (hexagonal) grid image that a button and a couple of parameters produce and which is loaded into the panel as the background image.
That panel has a mouseclick event which works out from the coordinates which hex has been selected and the user can then add a tile to that hex, in the form of a png loaded into a picturebox which is located at the appropriate position.
Many tiles can be added to the grid. However, once a tile has been added, my mouseclick event no longer fires when clicking on that hex. (Presumably because you're now clicking on the picture box and not the panel?)
How can I best go about fixing this issue, as I want users to be able to reselect tiles they've placed and manipulate (rotate) or delete them.
If I don't have the ability to determine which hex is clicked, I can't identify which picture box is required...

The only solution I've managed to think of would be to add a mouseclick event to each control added, but I don't know how many hexes I might have so how can I plan how many events I'd need? (I suspect I've got a mental block somewhere)

Upvotes: 0

Views: 72

Answers (2)

GavinP
GavinP

Reputation: 687

Thanks to Sinatr.

I solved the problem by adding an eventhandler to each picturebox, and pointing that mouseclick to the same method which used the control name to identify which one was clicked. Since my picture boxes are named consistently to reflect the hex they occupy, it was straight forward.

The key was casting the sender as a control.

private void SetClickedHexFromPB(object sender, EventArgs e)
    {
        string name = (sender as Control).Name;

Upvotes: 0

Developer Nation
Developer Nation

Reputation: 384

one possible solution is to loop through the panel in which all picturebox exists and then create mouseclick event for those picturebox and then you can do your stuff in the event.

Upvotes: 1

Related Questions