Click here to Skip to main content
15,882,894 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a window form, and it includes several controls (type is panel),
the controls may overlap for each other, and when I click one of the controls,
I would check how many controls are overlapping on this clicked control.
My question is how to detect in this way ? Thanks of your help.

Each control has a Top, Left, Width and Height property. When a user selects a control, you will need to determine these properties, and then cycle through all the other controls to check if there is an intersection. You can use a foreach loop on the form's controls, something like this:-
Rectangle sr = new Rectangle(panel1.Left, panel1.Top, panel1.Width, panel1.Height);
foreach (Control ctrl in this.Controls)
	if (ctrl is Panel && ctrl != panel1)
		Rectangle r = new Rectangle(ctrl.Left, ctrl.Top, ctrl.Width, ctrl.Height);
		if (sr.IntersectsWith(r))
			//do your stuff

I have just used panel1 as an example. You should substitute for your selected panel.
Share this answer
Sergey Alexandrovich Kryukov 20-Mar-12 2:25am    
Wayne, I appreciate that you actually answered the question, but I always start with the question "why?".
The whole idea is usually based on a wrong approach to the layout design.
So, instead of answering this supposedly useless question, I prefer to advise how to avoid the problem in first place.

Please see my answer.
Sports Kuo 20-Mar-12 7:25am    
Hi Wayne,
Thanks so much of your suggestion, it should be helpful to solve my problem. I originally suppose there is one and simple system_built function to reach the same result, and now I understand it should be done step by step, thanks very much.

Hi SA,
Always thanks of your comments. In my project, it indeed allow user to put controls overlap for each other in form, so I have to do the detection of 'overlap' issue for further process. Thanks, and need your help always.
Sports Kuo 21-Mar-12 8:10am    
Hi Wayne,

I've updated my program according to your example code, however, it shows the result of 'if (sr.IntersectsWith(r))' is always 'true' !

Of course, I test on my program with same code design, I found the location of sr and r is same with (0,0), should I lose some step for it ?

Thanks of your kind and prompt response !
Wayne Gaylard 21-Mar-12 9:05am    
Sorry, I made a mistake using the ClientRectangle property. Please see my updated answer.
Sports Kuo 21-Mar-12 21:21pm    
Hi Wayne,
It works very well according to your update, thanks very much.
Hi Sports,

This is easy to do, but usually overlapping of the controls is a result of wrong layout design. If you want to avoid overlapping, you should not waste time of detecting it. Right design won't allow such things. It should be based on hierarchical panels, docking and padding. By the way, it also solves the problem of variable form's size and screen resolution.

Please see my past answer:
Zom Out malfunctions when Screen resolution changes[^].

In this one you can find a rudimentary code sample:
how to dock button so that it can adjust with the form[^].

See also this one:
GUI Apperance - C#.Net[^].

Good luck,
Share this answer
Sports Kuo 20-Mar-12 7:28am    
Hi SA,

Thanks of your information, I'll refer to it later, if any question will let you know at once.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900