public partial class MainWindow : Window{ public { InitializeComponent(); } MainWindow()public MainWindow()private void filesDropped(object sender, DragEventArgs e) if (e.Data.GetDataPresent(DataFormats.FileDrop)) { string[] droppedFilePaths = e.Data.GetData(DataFormats.FileDrop, true) as string[]; foreach (string droppedFilePath in droppedFilePaths) { ListBoxItem fileItem = new ListBoxItem(); fileItem.Content = System.IO.Path.GetFileNameWithoutExtension(droppedFilePath); fileItem.ToolTip = droppedFilePath; DropListBox.Items.Add(fileItem); } } |
Drag n Drop files into listbox but not duplicate files?
I am building a drag and drop that will drag a file into a listbox and "tooltip" the file location. Right now, I am trying to get it so that if the file is already there, it won't add it, but give a pop up saying it is there. Here is my code.

Dan SullivanRank #3,426 · 422 reputationPosted Aug 3, 2011, 8:51 AM EDT
~Dan
lynn osler23 reputationPosted Aug 3, 2011, 8:55 AM EDT
Dan SullivanRank #3,426 · 422 reputationPosted Aug 3, 2011, 9:02 AM EDT
If you can't, you could always iterate through the collection for an item with the same tooltip property.
~Dan
lynn osler23 reputationPosted Aug 3, 2011, 9:10 AM EDT
if (!DropListBox.Items.Contains(fileItem))
{
DropListBox.Items.Add(fileItem);
}
else
{
MessageBox.Show("File Exists");
}
Dan SullivanRank #3,426 · 422 reputationPosted Aug 3, 2011, 9:15 AM EDT
foreach (ListboxItem li in DropListBox.Items)
{
if(li.ToolTip == fileItem.tooltip)
{
MessageBox.Show("It exists!");
break;
}
}
lynn osler23 reputationPosted Aug 3, 2011, 9:26 AM EDT
Dan SullivanRank #3,426 · 422 reputationPosted Aug 3, 2011, 9:28 AM EDT
~Dan
Sam HobbsRank #107 · 20.5K reputationPosted Aug 3, 2011, 10:00 AM EDT