Drag and Drop a list item to a button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • elmbrook
    New Member
    • Feb 2010
    • 15

    Drag and Drop a list item to a button

    Hi All

    I am trying to drag and drop text from a listbox to be the title of a button.

    Can someone please help or point me in the right direction?

    I thought that this should have worked but it does nothing. I have set Allow Drop on the button to true.

    Code:
                   private void listBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                int indexOfItem = listBox1.IndexFromPoint(e.X, e.Y);
                listBox1.DoDragDrop(listBox1.Items[indexOfItem], DragDropEffects.Copy);
            }
    
            private void btn1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
            {
    
    
                // change the drag cursor to show that listbox2 is ready and set to accept data entry
                // either to Copy or Move
                // make sure we aren't inadvertently passing one of the other objects allow such as a bitmap!
    
                if (e.Data.GetDataPresent(DataFormats.StringFormat) && (e.AllowedEffect == DragDropEffects.Copy))
                    e.Effect = DragDropEffects.Copy;
                else
                    e.Effect = DragDropEffects.Move;
    
                
            }
    
            private void btn1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
            {
                if (e.Data.GetDataPresent(DataFormats.StringFormat))
                {
                    btn1.Text = e.Effect.ToString();
    
                }
                
            }
            private void aaa_Load(object sender, EventArgs e)
            {
                listBox1.Items.Add("one");
                listBox1.Items.Add("two");
                listBox1.Items.Add("three");
            }
  • ThatThatGuy
    Recognized Expert Contributor
    • Jul 2009
    • 453

    #2
    check this link it will help

    Comment

    Working...