I have a ListBox which needs to react to the window being resized. Simple way of dealing with this is of course to call a method in the listbox from the resizing event of the parent window.
However I would like the code to deal with this to be inside the ListBox if possible (so it is completely self-contained).
The CellPaint and CellBackgroundPaint event fire too often to be useful.
I know I could use a timer to achieve this (check periodically to see if the width or height of the parent window has changed), but is there a way to do it without a timer? Or have I exhausted all the possibilities?
[quote=163896:@Markus Winter]The CellPaint and CellBackgroundPaint event fire too often to be useful.
I know I could use a timer to achieve this (check periodically to see if the width or height of the parent window has changed), but is there a way to do it without a timer? Or have I exhausted all the possibilities?
[/quote]
Use Cell TextPaint but cache the last size
in CellbackgroundEvent
If me.Width <> LastWidth Or Me.Height <> LastHeight Then
LastWidth = me.Width
LastHeight = me.Height
Raise MyResizedEvent
End if
I’m not sure what reactions you want. If it is just to resize when the window resizes just set the locks in the Inspector for the Listbox. Depending on which ones you lock the LlistBox will resize with the window. If you have specified the ColumnWidths to be dynamic, e.g. using % or * in the specification, the columns will also resize as the window is resized. No special code needed for this.
Perhaps we need more info on what you want the listbox to do when resizing.
A hybrid solution would be in CellBackgroundPaint, when a resize is detected to THEN start a short single shot timer so the code executes after the Painting is done.
What I do in such a case is to have a listbox subclass instance and a combobox instance in the window. The combobox instance is on top of the listbox - the exact position doesn’t matter, but it must show the red borders when moving the combobox (indicating that the listbox is now the parent of the combobox.
To get hold of the combox within the listbox subclass, in the Open event of the listbox subclass I loop over all controls in the window and check for the control’s Parent property and the one with Parent = Self is it. Now all code for the combobox can be put in the Listbox subclass (for reacting to combobox events one needs to use AddHandler).