How to customise the behaviour of a control when it is selected
-
I am developing a control that displays a grid of pictures. I want to be able to select a picture by clicking on it, and change its appearance (eg. with a border) to show that it is selected. I have created: 1. a class derived from UserControl that contains a PictureBox for the picture 2. a class derived from FlowLayoutPanel to contain the pictures. This overrides
CreateControlsInstance()
to return an instance of... 3. a class derived from Control.ControlCollection to contain the picture objects. This overrides theAdd(Control value)
method in order to add a handler to the Click event of the control being added to the collection. It also contains the click event handler, and this is where my problem lies:- It seems to me that the logical next step is for the click event handler to identify the control that has been clicked (no problem) and to call itsSelect()
method, which will then alter the appearance of the control as required. The problem is thatControl.Select()
cannot be overridden. This raises two questions: Q1: what does Control.Select() actually do? Q2: how do I implement the change in appearance of the control. Obviously I could just set theUserControl.BorderStyle
property from within the click handler in collection class, but surely an object should make its own decision as to how it looks when it is selected. I can only assume that I've misunderstood something, or am doing it all the wrong way. Can anyone advise?Dave