Coding Thread, Drag and drop buttons at runtime c# in Coding and Web Development; I'm interested in creating an interface in a program I have coded where a list of items is provided somehow ...
7th October 2010, 01:28 PM #1
Drag and drop buttons at runtime c#
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.
IDG Tech News
7th October 2010, 01:38 PM #2
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
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...
Last edited by LosOjos; 7th October 2010 at 01:42 PM.
Thanks to LosOjos from:
localzuk (7th October 2010)
7th October 2010, 02:15 PM #3
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
7th October 2010, 02:29 PM #4
Glad to be of help
Originally Posted by localzuk
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
7th October 2010, 03:03 PM #5
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;
7th October 2010, 04:06 PM #6
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.
7th October 2010, 04:12 PM #7
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?
Originally Posted by localzuk
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
7th October 2010, 05:02 PM #8
Ok, got it all working, drag+drop, resize and moving.
Thanks for the help. I garnered some more info from here Drag and Drop controls on Form and Drag and Drop between list boxes - Beginner's Tutorial - CodeProject too.
Thanks to localzuk from:
LosOjos (7th October 2010)
8th October 2010, 01:52 PM #9
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)
Last edited by localzuk; 8th October 2010 at 01:54 PM.
By clareq in forum How do you do....it?
Last Post: 8th September 2010, 02:37 PM
Last Post: 9th September 2009, 12:17 PM
By Millsy79 in forum Windows
Last Post: 31st July 2008, 05:26 PM
By conehead in forum Educational Software
Last Post: 10th June 2008, 12:17 PM
By ITWombat in forum *nix
Last Post: 9th June 2008, 07:14 PM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)