Custom WinForms Controls - Forcing “Always on Top”
Having read the following question from MattBerry on the student forum for the MCAD I am studying towards:
Does anyone know how to make a custom control the top most control on a
form i.e. its z-order.
I know forms create the z-order in the order the controls are created,
and I know you can use the control.BringToFront method after all
controls have been created.
What I want to know is, is it possible to set this z-order in the
custom control's code, so that it can be encapsulated?
It sparked my curiosity, can it be done? I have never tried anything like this myself. And yes, there is a side of me that would get REALLY pissed off with using a control that did this, but I thought “what the hell?” lets have a look and see what we can do.
Now, a bit of disclosure, it has been a while since I done any Windows development, so this may well be possible to do a lot easier, but hey, it took me 15 minutes :P
I had no real idea where to start as I only remember the most important properties/methods for WinForms controls. So I created a class for a new control, then sat there going through the Intellisense to see if any of them sparked my curiosity:
I pretty much set here looking at this for 5 minutes while I got reacquainted with the Control class.
Heading Down the Right Path
I came across a property that caught my eye:
Looks interesting right? I then checked out it’s article on MSDN. It turns out it tells you if this control is “top level” in the control hierarchy (i.e. is it contained) and got the nice sense of disappointment as I realised it would not give me that I really needed. But it did get me thinking, “if I know I am in a container, then I should be able to get a reference to the container, right?”
So I started scrabbling around to see if I can get a reference to the container control, Control.GetContainerControl sounds like it will do the job nicely, so I hacked together some code:
Yes, I agree its not too pretty, and could be more efficient etc, but it seems to do the work. So what is it doing?
- First we call the base method, as we tend to do that for most overrides to make sure we don’t miss any important base logic.
- We then determine if we are a top-level control, or a control within a hierarchy via GetTopLevel();
- If we are in a hierarchy, then we simply:
- Remove our control from the container’s Controls collection.
- Copy the rest of the controls in the container to a temp collection.
- Clear the collection.
- Add our control back to the container (now the first control).
- Add the controls we saved back to the collection.
A Quick Test
To give it a quick test, I added the following rendering logic to paint a box:
I then chucked our control on a form and added a button on top of it (you can right click and say “bring to front” if required).
When first added, it looks like it is not working, but that is because the ZOrderControl’s logic has not had a chance to fire.
Hit F6 to build and see what happens.
This has been far from tested and of course there may be issues with messing with the control hierarchy. I simply wanted to find a way to do the goal required. I tested obvious things like control events still firing etc, but use this at your own risk!