简体   繁体   中英

Dynamically generated controls issue

I have a form with a panel docked in it. I then dynamically create 15 panels (named: panel_n) and 15 pictureboxes (named: picturebox_n) on the primary panel (named ContainerPanel).

When dragging the any picturebox over a panel (panel_n) created using the relevant mouse events. I would like to get the panel's name that the picture box was dragged over. The mouse cursor seems to be captured.

I have tried creating a IMessageFilter interface, but there are still no events that trigger when dragging one of the pictureboxes over any one of the panels.

The ClientRectangle.IntersectsWith function also does not work as the co-ords are always 0,0.

All I need is the panel name where the picturebox was dragged over (preferably on the mouseup event)

If you give the pictureboxes an OnMouseDown event that says something like this:

(sender as PictureBox).DoDragDrop(sender, DragDropEffects.Copy);

Then you can set the panels' AllowDrop property to true, and in their OnDragDrop event, you can get their name like this:

string myName = (sender as Panel).Name;

Edit: Also, you need to give the panels an OnDragEnter event like this:

e.Effect = DragDropEffects.Copy;

Of course, you can change Copy to Move or Link or whatever is appropriate for what you're doing. It just changes the mouse-pointer icon that's used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM