+ Post New Thread
Results 1 to 9 of 9
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 ...
  1. #1

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,689
    Thank Post
    516
    Thanked 2,455 Times in 1,899 Posts
    Blog Entries
    24
    Rep Power
    833

    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.

  2. #2

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,439
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    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...
    Last edited by LosOjos; 7th October 2010 at 12:42 PM.

  3. Thanks to LosOjos from:

    localzuk (7th October 2010)

  4. #3

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,689
    Thank Post
    516
    Thanked 2,455 Times in 1,899 Posts
    Blog Entries
    24
    Rep Power
    833
    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

  5. #4

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,439
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Quote Originally Posted by localzuk View Post
    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

  6. #5

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,439
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    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):

    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;

  7. #6

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,689
    Thank Post
    516
    Thanked 2,455 Times in 1,899 Posts
    Blog Entries
    24
    Rep Power
    833
    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.

  8. #7

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,439
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Quote Originally Posted by localzuk View Post
    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

  9. #8

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,689
    Thank Post
    516
    Thanked 2,455 Times in 1,899 Posts
    Blog Entries
    24
    Rep Power
    833
    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.

  10. Thanks to localzuk from:

    LosOjos (7th October 2010)

  11. #9

    localzuk's Avatar
    Join Date
    Dec 2006
    Location
    Minehead
    Posts
    17,689
    Thank Post
    516
    Thanked 2,455 Times in 1,899 Posts
    Blog Entries
    24
    Rep Power
    833
    Now, the final part of this fun process is removing an item if it is right clicked.

    EDIT: Easily done.

    Code:
    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);
                    }
    Last edited by localzuk; 8th October 2010 at 12:54 PM.

SHARE:
+ Post New Thread

Similar Threads

  1. Drag and drop for disabled student
    By clareq in forum How do you do....it?
    Replies: 7
    Last Post: 8th September 2010, 01:37 PM
  2. Drag and Drop broken in OWA
    By ranj in forum Windows
    Replies: 2
    Last Post: 9th September 2009, 11:17 AM
  3. Cant drag and drop
    By Millsy79 in forum Windows
    Replies: 4
    Last Post: 31st July 2008, 04:26 PM
  4. click n drag music composing
    By conehead in forum Educational Software
    Replies: 8
    Last Post: 10th June 2008, 11:17 AM
  5. hotmail drag and drop not on Linux Firefox
    By ITWombat in forum *nix
    Replies: 1
    Last Post: 9th June 2008, 06:14 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •