Test Foro de elhacker.net SMF 2.1

Programación => Programación General => .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 (vbnet) [Seleccionar]

      ' Start a drag.
       foldertextbox.DoDragDrop( _
           foldertextbox.Text, _
           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 (vb.net) [Seleccionar]
(Info.Attributes And IO.FileAttributes.Directory) = IO.FileAttributes.Directory

Código (vb.net) [Seleccionar]
   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
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 (vbnet) [Seleccionar]
   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


CitarConversion 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 (vb.net) [Seleccionar]
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 (vbnet) [Seleccionar]
   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