Click here to Skip to main content
15,070,828 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have created a customized Panel which has a selectable Frame (with different width) and a selectable Caption-Area (with different height).
I have also modified the ParentControlDesigned for my need.
Now my question :
If I move a Control over this container - how can I limit the Drag-Drop-Position ?
In the moment it is possible to put the Control (for example a Button) directly over the Caption - I don't want it ...

What I have tried:

With googeling I found something with overwriting the OnDragOver- and OnDragEnter-Methods of my customized Designer - but all tries without success.
What I need is a useable AND understandable sample to go further. Perhaps someone can point me into the right direction ...

I have a Container-Control like a Panel or a GroupBox - but it is customized. There are areas around this Control to which a dragged Control from outside should not be moved - for example the Caption-area. Actually I can place an incoming Control (like a Button) directly over the caption - but this should be a forbitten area to where an incomming Control should not be placed. To clarify it with the example of the Form itself. Normally it is impossible to move or place a Control over the caption-area. The same I want to have at my Control ...

Additional :
I tried the suggestion of Maciej but it leads to nowhere ... The point is that in DESIGNMODE my Control doesn't ask for the drag-drop. What I tried now was do enable the functions with a customized ControlDesigner - here exists the method GetHitText with which you can activate underlying parts of the Control during DesignMode (for Example open a ComboBox or Switch Tabs of a TabControl and so on - I have a lot experience with that). But that doesn't work ... :-(
So I tried do do the same inside the customized ParentControlDesigner - there are also existing methods for Drag-Drop - but without success. I think this is the right place but I don't have useable Information about that - at this point the Internet AND Microsoft is very very poor.
The only possible Solution I found until now is a combination of a control with a Panel inside. Of course ... I could do it this way and that will work ... but that seams also very poor to me ...
And CodeProject also doesn't help me to get a step foreward ... :-(
Posted
Updated 13-Feb-21 5:16am
v6
Comments
Maciej Los 11-Feb-21 12:57pm
   
I'd suggest to disable drop operation by adding handler to below event:
private void control_DragEnter(object sender, DragEventArgs e){  e.Effect = DragDropEffects.None; }

BTW: Control.AllowDrop Property (System.Windows.Forms) | Microsoft Docs[^]
Ralf Meier 11-Feb-21 13:09pm
   
Thanks Maciej - I will try that later ...
But in my opinion is a functionality which comes from the ControlDesigner and not from the Control itself.
I can actually drag other Controls inside my Panel - but I can't prevent that at special (or better unwanted) positions ...
Maciej Los 12-Feb-21 2:45am
   
What about using MouseHover or MouseMove event(s)? If mouse is over a "Caption" or over the restricted area, reset drag-drop operation.
Maciej Los 12-Feb-21 2:55am
   
Another idea is to use DragEventArgs Class (System.Windows) | Microsoft Docs[^] to get cursor coordinates. Then, using Control.PointToScreen(Point) Method (System.Windows.Forms) | Microsoft Docs[^] allow/disallow drag-drop operation.

BTW: Take a look here: Enable Your Windows Forms Applications to Drag-and-Drop Data Objects[^] - table 2, which contains an order of sub-operations to perform drag-and-drop operation.
Ralf Meier 12-Feb-21 3:58am
   
That's an idea - of course. I haven't tried it that way until now.
What I don't understand is that there is no information or samples availible for this issue ... :-(
Maciej Los 12-Feb-21 4:30am
   
Suppose, you'll be the first person, who'll write short tip about that ;)
Ralf Meier 12-Feb-21 6:21am
   
If I have a Solution I will do that ... and earn my first points as Author ... ;-)
But first I must have a Solution ...
Ralf Meier 13-Feb-21 4:03am
   
I tried your suggestion but it leads to nowhere ... The point is that in DESIGNMODE my Control doesn't ask for the drag-drop. What I tried now was do enable the functions with a customized ControlDesigner - here exists the method GetHitText with which you can activate underlying parts of the Control during DesignMode (for Example open a ComboBox or Switch Tabs of a TabControl and so on - I have a lot experience with that). But that doesn't work ... :-(
So I tried do do the same inside the customized ParentControlDesigner - there are also existing methods for Drag-Drop - but without success. I think this is the right place but I don't have useable Information about that - at this point the Internet AND Microsoft is very very poor.
The only possible Solution I found until now is a combination of a control with a Panel inside. Of course ... I could do it this way and that will work ... but that seams also very poor to me ...
And CodeProject also doesn't help me to get a step foreward ... :-(
Maciej Los 14-Feb-21 7:23am
   
Ralf, i think that we can't help you without seeing your code (of control)... If there's several nested controls, your custom control doesn't know which nested control can "handle" drag-drop operation. Maybe you should expose event? Note: that's just a guess.

On the other hand... Have you tried to clean-up solution and build it again. Last few updates of VS 2019 were bit inaccurately. That makes that some bugs are removed via update, but the other bugs are becomming apparent.

Are you able to provide sample code that we can reproduce your issue?
[no name] 13-Feb-21 10:33am
   
Should not implementing DragOver Event help to solve that? I think there you have all the fredom also to check areas.
Control.DragOver Event (System.Windows.Forms) | Microsoft Docs[^]
Ralf Meier 13-Feb-21 10:43am
   
Hi ...
that is nearly the same as Maciej allready suggested to me. To clarify : my issue is the DesignTime-functionality - there it must work. The Drag-Events from the Control during Runtime will work (I'm sure).
During DesignTime (I suppose) the ParentControlDesigner must be the right address - but all my tries there (without any usable documetation from Microsoft or others) did not solve in any success ... :-(
[no name] 13-Feb-21 10:47am
   
Ok, I think I get what I was missing. 'At Design Time' is the problem if I understand. If true then I think it becomes complex, because at design time VS gets the control for d&d. Interesting question, I will dive into ;)

Q: Do you implemented IDesignerHost?
Ralf Meier 13-Feb-21 11:15am
   
I know that this is not an easy question ...
[no name] 13-Feb-21 11:05am
   
Ralf Meier 13-Feb-21 11:14am
   
I don't have implemented IDesignerHost ... but I created a customized ParentControlDesigner to my Control - I suppose that goes in the same direction ...
With the basic functions of the ControlDesigner I'm very familiar - only this part is a black hole for me ...
[no name] 14-Feb-21 6:51am
   
Ralf Meier 14-Feb-21 7:01am
   
Thanks a lot for your efforts - I will try it later and give a Feedback ... but basicly (first look onto it) it looks good :-)
[no name] 14-Feb-21 8:55am
   
I played a little bit with the code from the article from Henry Minute. Looks like this should do the job the only doubt I have, I think to understand that you allready tried a similar approach:
    public class TestControlDesigner : ParentControlDesigner
    {
        public override void Initialize(System.ComponentModel.IComponent component)
        {
            base.Initialize(component);

            if (this.Control is TestControl)
            {
                this.EnableDesignMode(((TestControl)this.Control).WorkingArea, "WorkingArea");
            }
        }
        
        protected override void OnDragOver(DragEventArgs de)
        {
            TestControl ctrl = ((TestControl)this.Control);

            // Screen coordinates to client
            Point point = ctrl.PointToClient(new Point(de.X, de.Y));

            // What control is 'under the mouse cursor'
            Control child= ctrl.GetChildAtPoint(point);

            // 'Visual' Debug at design time ;)
            ctrl.Caption = $"{child?.Name}  x={point.X}  y={point.Y}";

            // Only allow to drop on working area
            if (child == ctrl.WorkingArea)
            {
                de.Effect = DragDropEffects.Copy;
            }
            else
            {
                de.Effect = DragDropEffects.None;
            }
        }
Ralf Meier 17-Feb-21 4:10am
   
I were some days in hospital - now I'm back and now I will try your approach ...
[no name] 17-Feb-21 4:57am
   
I hope nothing serious. Take care and all the best.
Ralf Meier 17-Feb-21 6:09am
   
Gall bladder - I have had some stones too much ... and cyclic problems with them ... now (I hope) it is finished ...
[no name] 17-Feb-21 9:10am
   
Sounds painful. We say that drinking beer should help against these stones. Aber wird vermutlich nur eine Ausrede sein um einen Grund für das nächste Bier zu haben :-)
Ralf Meier 18-Feb-21 7:31am
   
Keine Nierensteine oder Blasensteine - Gallensteine - da hilft kein Bier ... :-(

Your Comments inspired me to go into a complete new direction. Also of course the Article from Henry Minute. But in the Moment I don't have MY SOLUTION - but I think I will get it ... ;-)
Virtual +5 to you from me too ...
[no name] 18-Feb-21 11:47am
   
Dann erst mal alles Gute für die Gesundheit, diese hat immer Vorrang.

Thanks for your V5 and glad it helped at least a little bit. Was also an interesting exercise for me ;)
Maciej Los 14-Feb-21 7:25am
   
This might be good direction to resolve Ralf's issue.
My virtual 5!
[no name] 14-Feb-21 8:56am
   
Thank you Maciej

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