Grab filename from drag&drop into ListView? How?

  • Thread starter Thread starter Ken Swanson
  • Start date Start date
K

Ken Swanson

Hi,

In VB.NET, how do I determine what filename has been dropped into a ListView?

In my ListView's DragEnter() event, I am trying this, which according to the Help,
should work:

MyFilename = e.Data.GetData("FileName").ToString
I have also tried e.Data.GetData(DataFormats.Text) and GetData(System.String)...
neither work. I keep getting "System.String[]" back as the filename.

I have used e.GetData.GetFormats() to get a list of the formats, and I see the
following:

Shell IDList Array
DragImageBits
DragContext
Shell Object Offsets
InShellDragLoop
FileDrop
FileName
FileNameW

I have tried using all of them to no avail. Some return "System.IO.MemoryStream".

Those last two lead me to believe that I should be able to specify GetData("FileName")
to get the filename, but it doesn't work. What am I doing wrong?

Ken
 
Ken,

It all depends on what "e" is. I'm only guessing, but is "e" a reference to
the ListView control that is raising the event? Why not put a breakpoint
here, get into the Command - Immediate window and rip the puppy apart with
stuff like

? e.text
? e.Data
? e.TryingToTellMe

You might just hit on the right combination of properties and methods.
 
Ken,
I take it you are dragging & dropping a file from Windows Explorer?

You will receive an array of string (System.String[]) which contains the
list of filenames (might be a list of 1 or a list of 100).

Try something like (untested):

Dim fileNames() as String
Dim MyFilename As String
fileNames= DirectCast(e.Data.GetData(DataFormats.FileDrop), String())

MyFilename = fileNames(0)

Charles Petzold's book "Programming Microsoft Windows With Microsoft Visual
Basic.NET" from MS Press, has a long discussion on using Drag & Drop and
what formats work with .NET and how to use them.

Hope this helps
Jay
 
Thanks for the responses. Both are good suggestions.

e is a reference to System.Windows.Forms.DragEventArgs.

I had tried to get an array of strings, since it looked like it was telling
me it was an array, but I'm new to VB and couldn't quite get it. Maybe
that DirectCast function below will help!

Thanks,

Ken

Ken,
I take it you are dragging & dropping a file from Windows Explorer?

You will receive an array of string (System.String[]) which contains the
list of filenames (might be a list of 1 or a list of 100).

Try something like (untested):

Dim fileNames() as String
Dim MyFilename As String
fileNames= DirectCast(e.Data.GetData(DataFormats.FileDrop), String())

MyFilename = fileNames(0)

Charles Petzold's book "Programming Microsoft Windows With Microsoft Visual
Basic.NET" from MS Press, has a long discussion on using Drag & Drop and
what formats work with .NET and how to use them.

Hope this helps
Jay

Hi,

In VB.NET, how do I determine what filename has been dropped into a
ListView?

In my ListView's DragEnter() event, I am trying this, which according to

the Help,
should work:

MyFilename = e.Data.GetData("FileName").ToString
I have also tried e.Data.GetData(DataFormats.Text) and
GetData(System.String)...

neither work. I keep getting "System.String[]" back as the filename.

I have used e.GetData.GetFormats() to get a list of the formats, and I see
the

following:

Shell IDList Array
DragImageBits
DragContext
Shell Object Offsets
InShellDragLoop
FileDrop
FileName
FileNameW

I have tried using all of them to no avail. Some return
"System.IO.MemoryStream".

Those last two lead me to believe that I should be able to specify
GetData("FileName")

to get the filename, but it doesn't work. What am I doing wrong?

Ken
 
Bingo! Much thanks, Jay.

I didn't know about DirectCast().

Ken


Ken,
I take it you are dragging & dropping a file from Windows Explorer?

You will receive an array of string (System.String[]) which contains the
list of filenames (might be a list of 1 or a list of 100).

Try something like (untested):

Dim fileNames() as String
Dim MyFilename As String
fileNames= DirectCast(e.Data.GetData(DataFormats.FileDrop), String())

MyFilename = fileNames(0)

Charles Petzold's book "Programming Microsoft Windows With Microsoft Visual
Basic.NET" from MS Press, has a long discussion on using Drag & Drop and
what formats work with .NET and how to use them.

Hope this helps
Jay

Hi,

In VB.NET, how do I determine what filename has been dropped into a
ListView?

In my ListView's DragEnter() event, I am trying this, which according to

the Help,
should work:

MyFilename = e.Data.GetData("FileName").ToString
I have also tried e.Data.GetData(DataFormats.Text) and
GetData(System.String)...

neither work. I keep getting "System.String[]" back as the filename.

I have used e.GetData.GetFormats() to get a list of the formats, and I see
the

following:

Shell IDList Array
DragImageBits
DragContext
Shell Object Offsets
InShellDragLoop
FileDrop
FileName
FileNameW

I have tried using all of them to no avail. Some return
"System.IO.MemoryStream".

Those last two lead me to believe that I should be able to specify
GetData("FileName")

to get the filename, but it doesn't work. What am I doing wrong?

Ken
 
Back
Top