localzuk Posted October 7, 2010 Posted October 7, 2010 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.
LosOjos Posted October 7, 2010 Posted October 7, 2010 (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 October 7, 2010 by LosOjos 1
localzuk Posted October 7, 2010 Author Posted October 7, 2010 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
LosOjos Posted October 7, 2010 Posted October 7, 2010 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
LosOjos Posted October 7, 2010 Posted October 7, 2010 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;
localzuk Posted October 7, 2010 Author Posted October 7, 2010 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.
LosOjos Posted October 7, 2010 Posted October 7, 2010 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
localzuk Posted October 7, 2010 Author Posted October 7, 2010 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. 1
localzuk Posted October 8, 2010 Author Posted October 8, 2010 (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 October 8, 2010 by localzuk
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now