How to determine the drag and drop destination?

T

Thi

Hi,

I am trying to develop an application that allows the users to drag a few
file(s) from a zip archive to a destination.

My question is, how do i determine where the drop destination is because i
need to do some validation checking before extracting the files from the zip
archive (e.g. validate archive password).

Any help or some sample codes would be very much appreciated.

Thanks

Thi
www.elmbrook.co.nz
www.backpackpro.co.nz
 
G

Guest

You can't know the destination because that's internal to the drop target.
All it does is ask you for the file list. Best you can hope for is delaying
generation of the physical files until the drop occurs (rather than when the
drag occurs).

The DataObject class (the default implementer of IDataObject) doesn't
support delay-rendering like that, you must create the physical files before
the call to DoDragDrop if you use the DataObject (or use a string array with
DataFormats.FileDrop).

If you want to delay extraction until when the drop occurs you can create a
class that implements IDropTarget, implement GetFormats to return at list of
formats containing at least DataFormats.FileDrop, implement GetDataPresent to
return true for each format type you support (again, at least
DataFormats.FileDrop), then implement GetData that is called when the drop
occurs to extract the files. Following is an example implementation of
DataObject that only supports file drops and generates a C:\test.txt when the
drop occurs (i.e. you can drag without dropping and the file will never get
created):

class MyDataObject : IDataObject {
#region IDataObject Members

public object GetData ( Type format ) {
throw new NotImplementedException();
}

public object GetData ( string format ) {
throw new NotImplementedException();
}

public object GetData ( string format, bool autoConvert ) {
if (format == DataFormats.FileDrop) {
string filePath = @"c:\test.text";
File.WriteAllText(filePath, String.Format("File created {0}\nThis is
some text.\nThis is the end of the file.", DateTime.Now.ToString()));
return new string[] { filePath };
}
return null;
}

public bool GetDataPresent ( Type format ) {
throw new NotImplementedException();
}

public bool GetDataPresent ( string format ) {
throw new NotImplementedException();
}

public bool GetDataPresent ( string format, bool autoConvert ) {
return (format == DataFormats.FileDrop);
}

public string[] GetFormats ( ) {
throw new NotImplementedException();
}

public string[] GetFormats ( bool autoConvert ) {
return new string[] { DataFormats.FileDrop };
}

public void SetData ( object data ) {
throw new NotImplementedException();
}

public void SetData ( Type format, object data ) {
throw new NotImplementedException();
}

public void SetData ( string format, object data ) {
throw new NotImplementedException();
}

public void SetData ( string format, bool autoConvert, object data ) {
throw new NotImplementedException();
}

#endregion
}

and can be used as follows:
DoDragDrop(new MyDataObject(), DragDropEffects.Copy);

You'll, of course, want to make it more useful than that... You'll run into
usage issues because you don't know what application is accepting the drop
and therefore can only present a form in your application (which is likely
behind the drop target).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top