Name of control at cursor position

pettrer

Well-known member
Joined
Sep 5, 2008
Messages
92
Programming Experience
10+
Hi all,

A quick question:

How do I get the name of the (topmost?) control at the cursor position?

The background: I implemented drag'n'drop from a dgv to a label and need to identify the control where the mouse button is released. I have enabled dropping on the label where the drop is going to take place. However, this label is the parent of say a hundred labels that are created dynamically in codebehind (I use this way to create a "dataview"). Therefore, I need to know which of these "child" labels the user drops the data onto.

I can get the mouseposition without problems (Me.PointToClient(Cursor.Position).ToString), but as I said, I can't get the name/text/ID of the control the user dropped the data on. Also, I ned to get the child control's name/ID/Text, not the parent label of course.

In addition, it seems as if I can't select the row I click on in the dgv anymore, after I implemented the DoDragDrop on the CellMouseDown event. Any ideas? Should I use another event?

Thanks a lot for any assistance!

Pettrer
 
Just write a single event handler for all the controls and use the AddHandler statement to attach it to the appropriate event when you create the controls, e.g.
VB.NET:
Dim newLabel As New Label

'...

AddHandler newLabel.DragDrop, AddressOf MyDragDropEventHandler
 
Hi,

Thanks for your help - very clever!

Just in case someone reads this thread in the future: The labels I created on the fly reside in a panel, not in another label. To the best of my knowledge, labels cannot hold other labels.

Thanks again!

Pettrer
 
Hello again,

Unfortunately, it doesn't seem as if this works as I expected. The label doesn't trigger the dragdrop, only the parent control (the panel) does. So how do I retrieve the ID or name of the label itself? I understnad that VB.Net can't possibly know what control of the two (parent or child) that I'm interested in, but I'd really want to be able to tell VB.Net that it's the label I want, not the panel.

Just in case I'm unclear:

------ PANEL START ------
Label1
Label 2 'Let's say I drop an item here, then I want VB.Net to know
Label 3 'that I dropped it on Label 2.
...
------ PANEL END ------

Right now I'm trying to solve the issue using the Y value, but it's rather ugly...

Warm regards,

Pettrer
 
DragDrop, etc. are members of the Control class, so ALL controls raise those events. Nothing will happen if you don't handle them though. A Label isn't going to raise a DragDrop event if you don't handle its DragEnter event because the dragging will end when you enter the control. I suggest that you do a bit of reading on drag & drop to see what events are involved and handle ALL the appropriate ones. The fact that the controls were created at run time matters not. You just use all the AddHandlers you need. Just make sure you have the corresponding RemoveHandlers or you'll end up with a resource leak.
 
Back
Top