Thread: Drag and Drop code (please help me fix)

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    Drag and Drop code (please help me fix)

    I don't have any idea what's wrong with this code. Can someone please help fix it, I have my entire form set to "Allow Drop = true". So i should just be able to drag a list view item anywhere and have the information displayed in the text boxes. Nothing happens when clicking a list view item and dragging it.

    Form Layout:

    ListView control on the left side with 5 names, 5 text boxes on the right side with labels corresponding to them. When a list view item is clicked and dragged anywhere on the form, their information should be displayed in the proper text boxes.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace Part_B
    {
        public partial class Form1 : Form
        {
            const int ASCIIO = 48;
    
            private struct people
            {
                public string fName;
                public string lName;
                public string number;
                public string years;
                public string degree;
           } 
            
            private people[] list = new people[5];
    
            public Form1()
            {
                InitializeComponent();
    
                list[0].fName = "John";
                list[0].lName = "Doe";
                list[0].number = "245-9087";
                list[0].years = "4";
                list[0].degree = "Computer Science";
    
                list[1].fName = "Jane";
                list[1].lName = "Smith";
                list[1].number = "222-2222";
                list[1].years = "1";
                list[1].degree = "Arts";
    
                list[2].fName = "Mike";
                list[2].lName = "Butler";
                list[2].number = "802-1233";
                list[2].years = "4";
                list[2].degree = "Economics";
    
                list[3].fName = "Alan";
                list[3].lName = "Kay";
                list[3].number = "455-3939";
                list[3].years = "3";
                list[3].degree = "Phsycology";
    
                list[4].fName = "Jill";
                list[4].lName = "Jacobs";
                list[4].number = "285-3944";
                list[4].years = "2";
                list[4].degree = "Computer Sciece";
    
            }
    
            private void listView1_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
            {
                listView1.DoDragDrop(listView1.FocusedItem.Index.ToString(), DragDropEffects.Copy);
            }
    
            private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
            {
                if (e.Data.GetDataPresent(typeof(string)))
                    e.Effect = DragDropEffects.Copy;
            }
    
            private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
            {
                int index = ((int)e.Data.GetData(DataFormats.Text).ToString()[0]) - ASCIIO;
    
                fName.Text = list[index].fName;
                lName.Text = list[index].lName;
                pNumber.Text = list[index].number;
                yCollege.Text = list[index].years;
                Degree.Text = list[index].degree;
            }
    
            private void Form_Load(object sender, EventArgs e)
            {
    
            }
    
            private void panel1_Paint(object sender, PaintEventArgs e)
            {
    
            }
        }
    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    I'm sure setting the form AllowDrop doesn't allow dropping in its subcontrols, only the form itself. You have to set the flag for all controls that should accept the dragged "files".

    When dragging an item, does the cursor change at all (so you know if ItemDrag and DoDragDrop works for you)?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [C] Drag and drop controls
    By pc2-brazil in forum Windows Programming
    Replies: 1
    Last Post: 09-02-2008, 02:41 AM
  2. Drag & Drop files into my (and other) apps
    By Mastadex in forum Windows Programming
    Replies: 1
    Last Post: 11-07-2007, 05:59 AM
  3. DataGrid drag and drop
    By gtriarhos in forum C# Programming
    Replies: 0
    Last Post: 10-11-2005, 12:36 PM
  4. Drag and Drop using CImageList, Device contexts and BitBlt
    By MJWhiteman2 in forum Windows Programming
    Replies: 0
    Last Post: 03-03-2005, 07:22 AM
  5. drag and drop
    By depsypher in forum Windows Programming
    Replies: 2
    Last Post: 01-05-2002, 10:02 PM