Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Drag and drop listbox. Store multiple values for each item.

$
0
0

Hi there,

I am trying to do a drag and drop file copy form with a listbox control.  So, thats pretty straightforward.  I want to just show the filename (not the whole path) to the user but I need to store the entire path to the file(s) somewhere for when the user hits submit.  So, I was looking at keeping a hashtable or dictionary in conjunction with the listbox but when I start allowing the user to delete single or multiple rows, I kind of run into an issue.  I'm thinking I can use a dictionary but there is a chance that someone could drag the same file twice thus I would have two identical values in the array.  Not sure what type of array would allow that....  Thanks for any help you can provide...  Chris

Basic code here:

 private void lstFiles_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                e.Effect = DragDropEffects.All;
            else
                e.Effect = DragDropEffects.None;
        }
        private void lstFiles_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
        {
            string[] s = (string[])e.Data.GetData(DataFormats.FileDrop, false);
            foreach (string file in s)
            {
                lstFiles.Items.Add(System.IO.Path.GetFileName(file));
            }
         }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            for (int x = lstFiles.SelectedIndices.Count - 1; x >= 0; x--)
            {
                int idx = lstFiles.SelectedIndices[x];
                lstFiles.Items.RemoveAt(idx);
            }
        }


Viewing all articles
Browse latest Browse all 12583

Trending Articles