0

In a vb.net , how can I block a drag and drop operation , if these condition are true :

1) If the user drag a folder

2) If the user drag a file that is not an Excel file or Word file

3) If the user drag more than 1 file

Thank you !

alex
  • 694
  • 3
  • 15
  • 35
  • Write your DragEnter event handler [similar to this](http://stackoverflow.com/a/29477851/17034). You are not happy if the array contains more than 1 element or the filename extension is not .doc, .docx, .xls or .xlsx. – Hans Passant May 26 '15 at 08:33
  • thank you , but what if the file is an Excel file , but has no extension ? I need to cover this case too and to permit dragging. – alex May 26 '15 at 08:56
  • No, you don't have to cover that case. It is technically possible, but starting up Excel in your DragEnter event handler just so you can check that it is a valid spreadsheet is drastically unpractical. DragEnter needs to be fast to be usable. – Hans Passant May 26 '15 at 09:01
  • but I need to cover that case. Because if all the detections described above are OK , I have to do something on a drag drop operation. and in order that my application will not fail I have to be sure that a file that is drag drop is a real Excel file. Imagine if the user drag a file that has .xls extension , but is not an excel file. According to your solution , all the detections inside DragEnter will result OK , but after my application will fail because the file with .xls extensions is not a real Excel file. – alex May 26 '15 at 09:08
  • Your DragDrop event handler will throw an exception. This is not a problem, your app will not fail. Did you actually try this or are you just assuming you'll have a problem? Try it. – Hans Passant May 26 '15 at 09:14
  • Inside DragDrop event , I have to do some actions with the file being dragged. Actually I'm inserting this file inside a spreadsheet control inside my form. I have tried that if the file is not a real excel file , my application produce a runtime error , and I have to close through task manager. – alex May 26 '15 at 09:17
  • Just don't use a control that doesn't allow you to use try/catch to detect a bad file. But surely it does, contact the vendor for support. – Hans Passant May 26 '15 at 09:21
  • Sorry friend , I understand your opinion. But I need to be able to detected an Word or Excel file regardless of extensions. Is there any way to do that ? – alex May 26 '15 at 09:23

1 Answers1

-1

Alex,

Your trying to accomplish much in a routine that requires very little code. The Drag event is supposed to return very quickly so that the UI can continue rendering and the UI remains responsive. For you to burden it with complicated file checking stuff would hider its responsiveness and introduce a very bad situation.

In the case of your UI crashing, that is supposed to be handled by you via error handling. The fact that your UI becomes inoperative means your not handling the situation properly.

The best approach is to constrain the user via file extension, a single file, not a folder and file size (meaning file size is less than 4MB for example). Then your code should assume the file is valid. Then pass the file to Excel (or what every the next process is), and let that process throw an exception. Then handle that exception and present a meaningful error information to the user like: "The file is not a valid Excel spreadsheet.".

Please understand that we are trying to help you implement a best practice and not a preferred practice; which is most cases makes your product unusable or unstable.

RashadRivera
  • 793
  • 10
  • 17