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.
Sam HobbsPosted Aug 3, 2011, 2:00 PM
Dan SullivanPosted Aug 3, 2011, 1:28 PM
~Dan
lynn oslerPosted Aug 3, 2011, 1:26 PM
Dan SullivanPosted Aug 3, 2011, 1:15 PM
foreach (ListboxItem li in DropListBox.Items)
{
if(li.ToolTip == fileItem.tooltip)
{
MessageBox.Show("It exists!");
break;
}
}
lynn oslerPosted Aug 3, 2011, 1:10 PM
if (!DropListBox.Items.Contains(fileItem))
{
DropListBox.Items.Add(fileItem);
}
else
{
MessageBox.Show("File Exists");
}
Dan SullivanPosted Aug 3, 2011, 1:02 PM
If you can't, you could always iterate through the collection for an item with the same tooltip property.
~Dan
lynn oslerPosted Aug 3, 2011, 12:55 PM
Dan SullivanPosted Aug 3, 2011, 12:51 PM
~Dan