Hallo,
ich habe ein Problem mit der Drag&Drop Funktion
Auf meiner Form habe ich 4 PictureBoxen. In einer
dieser PictureBoxen ist ein Bild, welches ich gern per
Drag&Drop in die anderen PictureBoxen verschieben will.
Es Funktioniert dahingehen schon, das mir das Bild dann
in der ZielPictureBox angezeigt wird. Da ich es aberverschieben,
soll es in der UrsprungsPictureBox nicht mehr zu sehen
sein.
Eine Lösung mit zwei PictureBoxen würde zum löschen diesen Code
benötigen:
If sender Is pic1 Then
pic2.Image = Nothing
ElseIf sender Is pic2 Then
pic1.Image = Nothing
End If
Aber 4 Kästchen hätten nun schon 16 verschiedene möglichkeiten,
die man alle abfangen müsste.
Nur Leider habe ich keine gute Idee wie man das machen kann ohne
jede der 16 möglichkeiten wie dort einzeln zu definieren.
Wäre sehr Dankbar wenn mir jemand dabei helfen könnte
Zum besseren verständniss ist heir noch der gesamte Code des Projekts
Public Class Form1
Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
pic1.AllowDrop = True
pic2.AllowDrop = True
pic3.AllowDrop = True
pic4.AllowDrop = True
End Sub
Private Sub PictureBox\_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic1.MouseDown, pic2.MouseDown, pic3.MouseDown, pic4.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim pic As PictureBox = CType(sender, PictureBox)
If Not pic.Image Is Nothing Then
pic.DoDragDrop(pic.Image, DragDropEffects.Move)
End If
End If
End Sub
Private Sub PictureBox\_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pic1.DragEnter, pic2.DragEnter, pic3.DragEnter, pic4.DragEnter
If (e.Data.GetDataPresent(DataFormats.Bitmap)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PictureBox\_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles pic1.DragDrop, pic2.DragDrop, pic3.DragDrop, pic4.DragDrop
Dim pic As PictureBox = CType(sender, PictureBox)
pic.Image = CType(e.Data.GetData(DataFormats.Bitmap), Bitmap)
If sender Is pic1 Then
pic2.Image = Nothing
ElseIf sender Is pic2 Then
pic1.Image = Nothing
End If
End Sub
End Class
ich habe ein Problem mit der Drag&Drop Funktion
Auf meiner Form habe ich 4 PictureBoxen. In einer
dieser PictureBoxen ist ein Bild, welches ich gern per
Drag&Drop in die anderen PictureBoxen verschieben will.
Es Funktioniert dahingehen schon, das mir das Bild dann
in der ZielPictureBox angezeigt wird. Da ich es
aberverschieben,
soll es in der UrsprungsPictureBox nicht mehr zu sehen
sein.
Ok, das ist sicherlich nicht schwer
ABER, wir sind hier in einem VB Forum, welches nicht das .NET ist!
Von daher findest du im Normalfall, hier keine Lösung!
Aber zu deinem Problem
Wieso machst du es dir so unnötig schwer?
Du kannst doch via Drag and Drop komplette Objecte verschieben!
Ein Object wiederrum hat Eigenschaften und diese mache dich zu nutzen.
Verschiebe nicht das Bild oder die Picturebox, sondern schiebe die Daten in eine Klasse und schiebe die dann
So kannst du auch genau überprüfen ob es definitiv Daten sind die von den Pictureboxen kommen! Hört sich kompliziert an ? Ist es nicht
Hier mal dein Source ein wenig abgeaendert
Public Class Form1
Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
pic1.AllowDrop = True
pic2.AllowDrop = True
pic3.AllowDrop = True
Pic4.AllowDrop = True
End Sub
Private Sub PictureBox\_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Pic1.MouseDown, Pic2.MouseDown, Pic3.MouseDown, Pic4.MouseDown
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim pic As PictureBox = DirectCast(sender, PictureBox)
If Not pic.Image Is Nothing Then
pic.DoDragDrop(New MyData(pic), DragDropEffects.Move)
End If
End If
End Sub
Private Sub PictureBox\_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Pic1.DragEnter, Pic2.DragEnter, Pic3.DragEnter, Pic4.DragEnter
If e.Data.GetDataPresent(GetType(MyData)) Then
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub PictureBox\_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Pic1.DragDrop, Pic2.DragDrop, Pic3.DragDrop, Pic4.DragDrop
Dim Data As MyData = DirectCast(e.Data.GetData(GetType(MyData)), MyData)
DirectCast(sender, PictureBox).Image = Data.PB.Image
Data.PB.Image = Nothing
End Sub
End Class
Friend Class MyData
Private MPB As PictureBox
Protected Friend Property PB() As PictureBox
Get
Return MPB
End Get
Set(ByVal value As PictureBox)
MPB = value
End Set
End Property
Sub New(ByVal P1 As PictureBox)
MPB = P1
End Sub
End Class