miroxlav
miroxlav

Reputation: 12194

.NET Windows Forms – determine clicked control

I would like to get reference of Control at any part of the form I clicked: form background, checkbox, text box, label, anything. My forms are in MDI environment, but I think it doesn't matter much. Imagine a simple "inspector window" allowing to oversee some aspects of open MDI forms.

Currently I'm getting that reference by reading Form.ActiveControl (or only Form if ActiveControl is null) of active MDI form – but of course, that works only with controls which can hold the focus. Reference to labels or group boxes cannot be obtained this way.

I know that distributing Click handler to every control will solve the problem, but it appears to be a bit heavyweight and cumbersome – distribute handlers when form gets focus and remove handlers when focus is lost. Is there some more elegant solution to get reference of clicked object?

I'm using VB.NET but if you wish to include some expression or code snippet, feel free to use C#, too.

Upvotes: 1

Views: 99

Answers (1)

Z .
Z .

Reputation: 12837

The Form class has a GetChildAtPoint() method that might be what you are looking for...

http://msdn.microsoft.com/en-us/library/a6zktd23(v=vs.110).aspx

Upvotes: 2

Related Questions