简体   繁体   中英

Windows Forms: how to drag and drop a .xml file on a TextBox?

I have a Windows Forms TextBox in which I want to allow the user to drag and drop a file from Windows Explorer. I would like to allow only for dropping an .xml file (path) on the TextBox. The way of testing the file format to be dropped, on the DragEnter event, is:

private void DragEnter(object sender, DragEventArgs e)
{
  if (e.Data.GetDataPresent(DataFormats.Text))
    e.Effect = DragDropEffects.Move;
  else
    e.Effect = DragDropEffects.None;
}

The DataFormats above doesn't contain Xml. If I use DataFormats.FileDrops, I allow any types of files to be dropped, as far as I understood. Any ideas? Thanks in advance!

you should check the DataFormats.FileDrop then get the file name(s) and verify the file extension, you can then set the DragDropEffects as you wish depending on how many files are dropped and their extension (in your case only 1 and xml file extension).

check this answer with a working example: https://stackoverflow.com/a/736883/559144

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM