Foro de elhacker.net

Programación => .NET (C#, VB.NET, ASP) => Mensaje iniciado por: Eleкtro en 21 Noviembre 2012, 15:01 pm



Título: (SOLUCIONADO) AllowDrop... como narices usarlo?
Publicado por: Eleкtro en 21 Noviembre 2012, 15:01 pm
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
  1.       ' Start a drag.
  2.        foldertextbox.DoDragDrop( _
  3.            foldertextbox.Text, _
  4.            DragDropEffects.Copy)

Muchas gracias


Título: Re: AllowDrop... como narices usarlo?
Publicado por: Keyen Night en 21 Noviembre 2012, 15:13 pm
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
  1. (Info.Attributes And IO.FileAttributes.Directory) = IO.FileAttributes.Directory

Código
  1.    Private Sub TextBox1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragDrop
  2.  
  3.        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
  4.  
  5.            Dim Objetos As String() = e.Data.GetData(DataFormats.FileDrop)
  6.  
  7.            TextBox1.Text = Objetos(0)
  8.  
  9.        End If
  10.  
  11.    End Sub
  12.  
  13.    Private Sub TextBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
  14.  
  15.        If e.Data.GetDataPresent(DataFormats.FileDrop) Then
  16.            e.Effect = DragDropEffects.All
  17.        End If
  18.  
  19.    End Sub


Título: Re: AllowDrop... como narices usarlo?
Publicado por: Eleкtro en 21 Noviembre 2012, 15:17 pm
Muchas gracias por la explicación y el ejemplo Keyen!!


Título: Re: AllowDrop... como narices usarlo?
Publicado por: Eleкtro en 21 Noviembre 2012, 16:14 pm
No he podido hacer la comparación entre directorio y archivo, ¿Que estoy haciendo mal?


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

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


Título: Re: AllowDrop... como narices usarlo?
Publicado por: Keyen Night en 21 Noviembre 2012, 16:19 pm
Debes crear el objeto FileInfo

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

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

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


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