Jump to content
EduGeek EdSec 2026 is Go! 27th Oct in Derby! Join us for a day of EdTech security focused talks, networking, and an evening social ×

Recommended Posts

Posted

I'm interested in creating an interface in a program I have coded where a list of items is provided somehow (be it a listview, datagridview, or a panel full of buttons etc...) and allow a user at runtime to click those items and drag them onto another panel, positioning them as they please, with the resulting panel being saved.

 

Now, the only way I can think of doing this involves using the cursor position overriding paint methods.

 

Is there a simpler way? I'd love to be able to also have the buttons be resizable on the panel.

 

Any ideas or pointers would be much appreciated. :)

Posted (edited)

This tutorial might be a good start: Drag and Drop between list boxes - Beginner's Tutorial - CodeProject

 

I'm sure from there you could build up to having resizable buttons etc with some tweaking

 

EDIT:

been having a little think about this one, I wonder if you could create containers to contain some buttons, start by dynamically filling the first container with buttons labelled whatever they would have been in the list, then when they are dragged over to the other container, add them to a dynamic array which stores there position, ready for loading at a later date...

 

It's all theory of course because I haven't tried it, but I think you'd use the same methods and properties as in the previous tutorial...

Edited by LosOjos
  • Thanks 1
Posted
That has set me off perfectly. I now can drag ListItems convert them into buttons, and have them appear on a panel. The location is currently out, but I can fix that :)
Posted
That has set me off perfectly. I now can drag ListItems convert them into buttons, and have them appear on a panel. The location is currently out, but I can fix that :)

 

Glad to be of help :)

 

This might be useful too, just an example of moving controls arround in a container (pinched from a c-sharpcorner member called enda.mcmanemy)DragDropButton.zip

Posted

Last thing just in case you're struggling to position the buttons, this is the code I used (you should get the gist so you can adapt it to your own code):

 

'Panel2' is the destination panel in my form
'e' is the DragEventArgs object (default name in a DrogDrop event)
'btn' is the newly created button

Point pos = splitContainer1.Panel2.PointToClient(new Point(e.X, e.Y));
               btn.Top = pos.Y;
               btn.Left = pos.X;

Posted
Cheers. I now have Drag+Drop and resizing sorted. Just to get moving working. Only issue here is that the drag+drop uses the same events on the panel as the move would.
Posted
Cheers. I now have Drag+Drop and resizing sorted. Just to get moving working. Only issue here is that the drag+drop uses the same events on the panel as the move would.

 

Again, just brainstorming as I haven't tried it, but could you maybe pass a CSV string in the DragDrop event and include within that a tag defining whether this is a new button or an existing one?

 

I'll attach the source of the little project I created for you to look at, might make more sense than how I'm explaining it. In mine, I had a collection of buttons on the left panel, then when they were dragged I passed a CSV string containing the button's text, width and height properties, which is then parsed in the relevant DragDrop event to create the new button: this is where I'm thinking you could add your tag (NOTE: only dragging 'button1' works, I stopped playing when you said you had got it working)

 

Drag and Drop Test.zip

Posted (edited)

Now, the final part of this fun process is removing an item if it is right clicked.

 

EDIT: Easily done. :)

 

Panel p = (Panel)sender;
               if (MessageBox.Show("Are you sure?", "Are you sure you want to remove this item?", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
               {
                   panel1.Controls.Remove(p);
               }

Edited by localzuk

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...