(SOLUCIONADO) AllowDrop... como narices usarlo?

(1/1)

Eleкtro:
Hola,

¿Alguien podría darme un ejemplo de como se puede arrastrar una carpeta a un textbox en un winform?

Parece que no es suficiente con la propiedad AllowDrop = True ... y esto tampoco:
Código
      ' Start a drag.
       foldertextbox.DoDragDrop( _
           foldertextbox.Text, _
           DragDropEffects.Copy)

Muchas gracias

Keyen Night:
Debes filtrar entre que es un archivo y una carpeta porque así recibirás cualquiera de los dos por igual, una de las formas de hacerlo es en el Evento DragEnter, crear un objeto FileInfo, con la ruta del archivo/carpeta recibido y verificar si el archivo posee el Attributes IO.FileAttributes.Directory. Recordando que Attributes es Flags, y se comprueba:

Código
(Info.Attributes And IO.FileAttributes.Directory) = IO.FileAttributes.Directory

Código
   Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
 
       If e.Data.GetDataPresent(DataFormats.FileDrop) Then
 
           Dim Objetos As String() = e.Data.GetData(DataFormats.FileDrop)
 
           TextBox1.Text = Objetos(0)
 
       End If
 
   End Sub
 
   Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
 
       If e.Data.GetDataPresent(DataFormats.FileDrop) Then
           e.Effect = DragDropEffects.All
       End If
 
   End Sub

Eleкtro:
Muchas gracias por la explicación y el ejemplo Keyen!!

Eleкtro:
No he podido hacer la comparación entre directorio y archivo, ¿Que estoy haciendo mal?


Código
   Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles foldertextbox.DragDrop
       If e.Data.GetDataPresent(DataFormats.FileDrop) Then
           Dim Objetos As String() = e.Data.GetData(DataFormats.FileDrop)
 
           Dim attributes = Objetos(0)
           If ((attributes And FileAttributes.Directory) = FileAttributes.Directory) Then
               MsgBox("es un dir")
           Else
               MsgBox("no es un dir")
           End If

Citar

Conversion from string "C:\Users\Administrador\Desktop\W" to type 'Long' is not valid.

Keyen Night:
Debes crear el objeto FileInfo

Código
Dim I As New FileInfo(Objetos(0))

Y de esa variable es que sacas el valor de Attributes.

Cita de: EleKtro H@cker en 21 Noviembre 2012, 16:14 pm

No he podido hacer la comparación entre directorio y archivo, ¿Que estoy haciendo mal?


Código
   Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles foldertextbox.DragDrop
       If e.Data.GetDataPresent(DataFormats.FileDrop) Then
           Dim Objetos As String() = e.Data.GetData(DataFormats.FileDrop)
 
           Dim attributes = Objetos(0)
           If ((attributes And FileAttributes.Directory) = FileAttributes.Directory) Then
               MsgBox("es un dir")
           Else
               MsgBox("no es un dir")
           End If


Navegación

[0] Índice de Mensajes