PDA

View Full Version : [VB.NET] DragDrop


wingman87
30-09-2008, 00:21
Allora, forse sono io che sono rincoglionito, ma sto impazzendo per fare un cavolo di dragdrop di file nel form. Alla fine preso dalla disperazione ho provato con un bel copia-incolla... ma non va! Questo l'esempio che ho copiato da MSDN:
Aprite un nuovo progetto windows e schiaffateci questo codice:
Public Class Form1
Private picture As Image
Private pictureLocation As Point

Public Sub New()
' Enable drag-and-drop operations.
InitializeComponent()
Me.AllowDrop = True
End Sub

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)

' If there is an image and it has a location,
' paint it when the Form is repainted.
If (Me.picture IsNot Nothing) And _
Not (Me.pictureLocation.Equals(Point.Empty)) Then
e.Graphics.DrawImage(Me.picture, Me.pictureLocation)
End If
End Sub

Private Sub Form1_DragDrop(ByVal sender As Object, _
ByVal e As DragEventArgs) Handles MyBase.DragDrop
' Handle FileDrop data.
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
' Assign the file names to a string array, in
' case the user has selected multiple files.
Dim files As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
Try
' Assign the first image to the 'picture' variable.
Me.picture = Image.FromFile(files(0))
' Set the picture location equal to the drop point.
Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If

' Handle Bitmap data.
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
Try
' Create an Image and assign it to the picture variable.
Me.picture = CType(e.Data.GetData(DataFormats.Bitmap), Image)
' Set the picture location equal to the drop point.
Me.pictureLocation = Me.PointToClient(New Point(e.X, e.Y))
Catch ex As Exception
MessageBox.Show(ex.Message)
Return
End Try
End If

' Force the form to be redrawn with the image.
Me.Invalidate()
End Sub

Private Sub Form1_DragEnter(ByVal sender As Object, _
ByVal e As DragEventArgs) Handles MyBase.DragEnter
' If the data is a file or a bitmap, display the copy cursor.
If e.Data.GetDataPresent(DataFormats.Bitmap) _
Or e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub

End Class
L'esempio è preso da qui: LINK (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.allowdrop.aspx)
Praticamente si dovrebbe essere in grado di trascinare un file immagine sul form e questa dovrebbe diventare lo sfondo della stessa.

wingman87
01-10-2008, 00:04
Up

wingman87
02-10-2008, 00:05
Up.
Qualcuno potrebbe provare il codice e dirmi se funziona? Perché inizio a pensare che il problema non sia quello... Cmq la versione che uso è la 2005

MarcoGG
06-10-2008, 20:43
Io l'ho provato su VB 2008 con un .bmp, .jpg, .gif e direi decisamente che funziona.
Inoltre sulla parte destra della pagina che hai linkato è espressamente detto che :
This page is specific to Microsoft Visual Studio 2008/.NET Framework 3.5.
Other versions are also available for the following:
Microsoft Visual Studio 2003/.NET Framework 1.1
Microsoft Visual Studio 2005/.NET Framework 2.0
.NET Framework 3.0
;)

wingman87
06-10-2008, 22:23
Io l'ho provato su VB 2008 con un .bmp, .jpg, .gif e direi decisamente che funziona.
Inoltre sulla parte destra della pagina che hai linkato è espressamente detto che :
This page is specific to Microsoft Visual Studio 2008/.NET Framework 3.5.
Other versions are also available for the following:
Microsoft Visual Studio 2003/.NET Framework 1.1
Microsoft Visual Studio 2005/.NET Framework 2.0
.NET Framework 3.0
;)
Doh! Grazie mille, adesso allora provo l'esempio per la mia versione.
Che svista colossale! :D

wingman87
06-10-2008, 22:28
No, non va neanche così. Mi sa che devo reinstallare tutto perché c'è qualcosa che non va.
Grazie dell'aiuto

EDIT: Ho reinstallato sia VB che il framework (3.5) ma non va... Qualche idea?

MarcoGG
07-10-2008, 10:49
No, non va neanche così. Mi sa che devo reinstallare tutto perché c'è qualcosa che non va.
Grazie dell'aiuto

EDIT: Ho reinstallato sia VB che il framework (3.5) ma non va... Qualche idea?

Provato anche con un'applicazione FW 2.0, sempre da VB2008, nessun problema, funziona. Chiaramente ho scelto la pagina che si riferiva a VS2005 / FW2.0.
Nota : se usi VB2005, puoi aver anche installato FW3.5, ma non potrai mai creare applicazioni FW3.5. Potrai solo eseguirle. In ogni caso penso sia proprio un problema della tua installazione di VS... Ma non saprei proprio cosa possa essere. Se puoi posta il msg d'errore del Fw...

wingman87
07-10-2008, 13:36
Il fatto è che non da nessun errore... Semplicemente quando provo a passare l'immagine sul form non succede nulla, il simbolo del mouse resta quel cerchio sbarrato, come se non ci fosse nessun dragdrop.
Può essere che uso la versione Express? Ma con VS2005 che framework posso usare?
Al max metterò il 2008, ho una copia di VS2008 Professional Beta 2 presa a una conferenza

MarcoGG
07-10-2008, 13:58
Il fatto è che non da nessun errore... Semplicemente quando provo a passare l'immagine sul form non succede nulla, il simbolo del mouse resta quel cerchio sbarrato, come se non ci fosse nessun dragdrop.
Può essere che uso la versione Express? Ma con VS2005 che framework posso usare?
Al max metterò il 2008, ho una copia di VS2008 Professional Beta 2 presa a una conferenza


Visual Basic 2005 va su FW 2.0. Perciò usa il codice specifico e in teoria dovrebbe funzionare ( io su VB2008, se creo una App con FW 2.0 con quel codice non ho problemi ).
Del FW 3.0 francamente non so quasi nulla, dato che sono passato direttamente da VS2003 a 2008 ( FW 3.5 ).
Io dalla Beta2 di VS2008 ne starei alla larga. Fossi in te starei su VS 2005 + FW 2.0.
Se invece trovi qualcuno che ti "presta" un VS2008 Full, vai decisamente su quello, e puoi scegliere di creare applicazioni 2.0, 3.0, o 3.5.
Il massimo è il FW 3.5 SP1, che non ho ancora provato... ;)

wingman87
07-10-2008, 19:07
Niente da fare, ho provato a scaricare il framework 2.0 e a installarlo ma mi dice subito che è già installato con il sistema operativo, non me lo fa neanche ripristinare... Boh, proverò a scaricare VB2008 Express.

wingman87
08-10-2008, 14:03
Funziona!!! Evvai!!! Grazie dell'aiuto. Non so quale fosse il problema, comunque ho disinstallato tutto e poi ho installato VB2008 Express con SP1 e adesso funziona!