Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1364to1368
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Drag and Drop in Userform
05.06.2014 10:32:57
Ivonne
Hi,
habe eine Userform der beim Start 30 Images hinzugefügt und eine Grafik zugefügt werden,außerdem wird im Tag des Images die Bildadresse hinterlegt.
Das ist soweit alles in Ordnung.
Daneben gibt es noch feste Image (ab Image40)
Jetzt möchte ich folgendes erreichen, per Drag and Drop oder wenn nicht möglich per Klick auf eines der 30 Images soll der Tag in eine Variable geschrieben werden und bei Klick auf eines der festen Image soll dort das Bild zugefügt werden.
Im Testversuch mit manuell erzeugten Image habe ich das per Klick hinbekommen, nur bei den per Code erzeugten Image haut das nicht hin.
Wobei mir wenn möglich Drag and Drop lieber wäre.
gruss Ivonne

16
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
Bitte Beispielmappe hochladen! (owT)
05.06.2014 11:27:09
EtoPHG

AW: Bitte Beispielmappe hochladen! (owT)
05.06.2014 14:39:01
Ivonne
Hi,
eine Beispielmappe kann ich nicht hochladen,denn da müßten ja auch alle Grafiken rein,sonst gibt es Fehler.
Mein Code für die Erzeugung der Image sieht so aus
Private Sub UserForm_Initialize()
Dim i
Dim k
Dim objImage As Image
Dim strfile
Dim strname
k = 0
For i = 1 To 30
strfile = Worksheets("Tabelle2").Cells(i, 1).Value
strname = Worksheets("Tabelle2").Cells(i, 5).Value
Set objImage = UserForm2.Controls.Add("Forms.Image.1")
With objImage
.Name = "Image" & i
.Left = k
.Top = 389
.Width = 30
.Height = 40
.PictureSizeMode = fmPictureSizeModeStretch
.Picture = LoadPicture(strfile)
.Tag = strfile
.ControlTipText = strname
End With
k = k + 30
Next
End Sub
der ist auch in Ordnung und macht das was er soll.
Jetzt geht es darum die Grafik per Klick oder Drag and Drop auf ein anderes Image zu übertragen.
gruss Ivonne

Anzeige
AW: Bitte Beispielmappe hochladen! (owT)
05.06.2014 15:07:33
Ewald
Hallo Ivonne,
falls das Klickereignis reicht dann vielleicht so
in ein allgemeines Modul
Public picad As String
Public strctt As String
Public cImage() As New clsImage
in ein Klassenmodul mit Namen clsImage
Private Sub Image_Click()
picad = Image.Tag
strctt = Image.ControlTipText
End Sub
in die Userform
Private Sub UserForm_Initialize()
Dim i
Dim k
Dim objImage As Image
Dim strfile
Dim strname
Dim iCounter
Dim IM As Control
Dim ImageCount1 As Integer
k = 0
For i = 1 To 30
strfile = Worksheets("Tabelle2").Cells(i, 1).Value
strname = Worksheets("Tabelle2").Cells(i, 5).Value
Set objImage = UserForm2.Controls.Add("Forms.Image.1")
With objImage
.Name = "Image" & i
.Left = k
.Top = 389
.Width = 30
.Height = 40
.PictureSizeMode = fmPictureSizeModeStretch
.Picture = LoadPicture(strfile)
.Tag = strfile
.ControlTipText = strname
Debug.Print .Name
Debug.Print .Tag
End With
k = k + 30
Debug.Print Controls.Count
Next
For Each IM In UserForm2.Controls
If TypeName(IM) = "Image" Then
ImageCount1 = ImageCount1 + 1
If ImageCount1 > 11 And ImageCount1 
wenn es funktioniert die Debug.Print entfernen
für die festen Image für jedes dann folgenden Code
Private Sub Image40_Click()
If picad  "" Then
Image40.Picture = LoadPicture("")
Image40.Picture = LoadPicture(picad)
Image40.ControlTipText = strctt
End If
UserForm2.Repaint
End Sub
ob man in der Klasse auch Drag and Drop benutzen kann, kann och jetzt nicht sagen
Gruß Ewald

Anzeige
AW: Bitte Beispielmappe hochladen! (owT)
05.06.2014 19:28:10
Ivonne
Hi Ewald,
das sieht mit Klick schon sehr gut aus, nur klappt dies nicht bei allen per Code eingefügten Image
Weißt du woran dies liegen kann.
gruss Ivonne

AW: Bitte Beispielmappe hochladen! (owT)
05.06.2014 19:56:28
Ewald
Hallo Ivonne,
dann stimmt deine interne Reihenfolge nicht und deshalb ist auch das Debug.Print drin.
Öffne im VB-editor das Direktfenster und lösche den gesamten Inhalt.
Starte dann die Userform und schließe sie wieder.
Im Direktfenster kannst du jeweils an dritter Stelle die interne Reihenfolge ablesen
Die Anfangszahl(eventuell -1) und die Endzahl(+1) mußt du dann hier eintragen

If ImageCount1 > 11 And ImageCount1 
dann sollte es funktionieren
aber nicht vergessen, wenn alles funktioniert alle Debug.Print löschen
Gruß Ewald

Anzeige
AW: Bitte Beispielmappe hochladen! (owT)
05.06.2014 23:21:19
Ivonne
Hi Ewald,
habe es jetzt hinbekommen,funktioniert mit Klick bei allen Images.
habe nur festgestellt, das ein azsgewähltes Image immer bestehen bleibt auch wenn man nichts ausgewählt hat,kann man dies ändern.
gruss Ivonne

AW: Drag and Drop in Userform
05.06.2014 14:35:34
Nepumuk
Hallo,
eine Beispielmappe wäre wirklich hilfreich, dann müssen wir das Ganze nicht nachbauen.
Hier mal ein einfaches Beispiel für Drag&Drop eines Bildes von Image1 nach Image2. Da muss natürlich noch eine richtige Fehlerbehandlung rein !!!
' **********************************************************************
' Modul: UserForm1 Typ: Userform
' **********************************************************************

Option Explicit

Private Sub CommandButton1_Click()
    Call Unload(Object:=Me)
End Sub

Private Sub Image1_BeforeDragOver( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, ByVal Y As Single, _
        ByVal DragState As MSForms.fmDragState, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    Cancel = True
    Effect = fmDropEffectMove
    
End Sub

Private Sub Image2_BeforeDragOver( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, ByVal Y As Single, _
        ByVal DragState As MSForms.fmDragState, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    Cancel = True
    Effect = fmDropEffectMove
    
End Sub

Private Sub Image1_MouseDown( _
        ByVal Button As Integer, _
        ByVal Shift As Integer, _
        ByVal X As Single, _
        ByVal Y As Single)

    
    Dim objDataObject As DataObject
    
    If Button = 1 Then
        
        Set objDataObject = New DataObject
        Call objDataObject.SetText(Text:=Image1.Name)
        Call objDataObject.StartDrag(OKEffect:=fmDropEffectMove)
        Set objDataObject = Nothing
        
    End If
End Sub

Private Sub Image2_BeforeDropOrPaste( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Action As MSForms.fmAction, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, _
        ByVal Y As Single, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    With Controls(Data.GetText)
        
        Set Image2.Picture = .Picture
        
        Set .Picture = Nothing
        
        Repaint
        
    End With
End Sub

Das ist natürlich kein echtes Drag&Drop denn das DataObject von MS-Forms kann nur Text speichern und wir haben ja ein Bild.
Gruß
Nepumuk

Anzeige
AW: Drag and Drop in Userform
05.06.2014 19:19:32
Ivonne
Hi Nepumuk,
irgendwie komme ich mit dem Code nicht klar, was ist das feste Image und welches das per Code erzeugte.
Muß ich dann den Code auch für jedes Image schreiben oder geht das auch anders.
gruss Ivonne

AW: Drag and Drop in Userform
05.06.2014 20:13:00
Nepumuk
Hallo,
welchen Namen hat das Ziel-Image? Sollen die Bilder kopiert oder verschoben werden? Wenn verschoben, nur in eine Richtung oder auch wieder zurück? Der obige Code ist nur ein Beispiel, in der Klasse der Images sieht der ein bisschen anders aus.
Gruß
Nepumuk

AW: Drag and Drop in Userform
05.06.2014 21:56:22
Nepumuk
Hallo,
hier mal ein Beispiel wie das final aussehen würde. Bilder werden nur kopiert, bleiben also in der Auswahl erhalten. Das Ziel-Image hat bei mir den Namen "Image0":
' **********************************************************************
' Modul: clsImage Typ: Klassenmodul
' **********************************************************************

Option Explicit

Private WithEvents mobImage As MSForms.Image

Private Sub Class_Terminate()
    Set Image = Nothing
End Sub

Friend Property Get Image() As MSForms.Image
    Set Image = mobImage
End Property

Friend Property Set Image(ByRef probjImage As MSForms.Image)
    Set mobImage = probjImage
End Property

Private Sub mobImage_BeforeDragOver( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, _
        ByVal Y As Single, _
        ByVal DragState As MSForms.fmDragState, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    Cancel = True
    Effect = fmDropEffectCopy
    
End Sub

Private Sub mobImage_MouseDown( _
        ByVal Button As Integer, _
        ByVal Shift As Integer, _
        ByVal X As Single, _
        ByVal Y As Single)

    
    Dim objDataObject As DataObject
    
    If Button = 1 Then
        
        Set objDataObject = New DataObject
        Call objDataObject.SetText(Text:=Image.Name)
        Call objDataObject.StartDrag(OKEffect:=fmDropEffectCopy)
        Set objDataObject = Nothing
        
    End If
End Sub

' **********************************************************************
' Modul: UserForm1 Typ: Userform
' **********************************************************************

Option Explicit

Private mobjImageClassCollection As Collection

Private Sub Image0_BeforeDragOver( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, ByVal Y As Single, _
        ByVal DragState As MSForms.fmDragState, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    Cancel = True
    Effect = fmDropEffectCopy
    
End Sub

Private Sub Image0_BeforeDropOrPaste( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Action As MSForms.fmAction, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, _
        ByVal Y As Single, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    With Controls(Data.GetText)
        
        Set Image0.Picture = .Picture
        
        Repaint
        
    End With
End Sub

Private Sub Image0_MouseUp( _
        ByVal Button As Integer, _
        ByVal Shift As Integer, _
        ByVal X As Single, _
        ByVal Y As Single)

    
    'Rechtsklick zum Löschen des Bildes
    
    If Button = 2 Then
        
        Set Image0.Picture = Nothing
        
        Repaint
        
    End If
End Sub

Private Sub UserForm_Activate()
    
    Dim lngRow As Long
    Dim sngLeft As Single
    Dim strFile As String, strName As String
    Dim objImage As MSForms.Image
    Dim objImageClass As clsImage
    
    Set mobjImageClassCollection = New Collection
    
    With Worksheets("Tabelle2")
        
        For lngRow = 1 To 30
            
            strFile = .Cells(lngRow, 1).Value
            strName = .Cells(lngRow, 5).Value
            
            Set objImage = Controls.Add(bstrProgID:="Forms.Image.1", _
                Name:="Image" & CStr(lngRow))
            
            With objImage
                .Left = sngLeft
                .Top = 389
                .Width = 30
                .Height = 40
                .PictureSizeMode = fmPictureSizeModeStretch
                Set .Picture = LoadPicture(strFile)
                .Tag = strFile
                .ControlTipText = strName
            End With
            
            Set objImageClass = New clsImage
            
            Set objImageClass.Image = objImage
            
            Call mobjImageClassCollection.Add(Item:=objImageClass)
            
            sngLeft = sngLeft + 30
        Next
    End With
End Sub

Private Sub UserForm_Terminate()
    Set mobjImageClassCollection = Nothing
End Sub

Gruß
Nepumuk

Anzeige
AW: Drag and Drop in Userform
05.06.2014 23:06:11
Ivonne
Hi Nepumuk,
genial, so habe ich mir das vorgestellt, habe ein festes Image umbenannt auf Image0 und es funktioniert prima.
Was noch fehlt ist der Controltiptext, auch der sollte mit übernommen werden.
Aber nun kommt für mich das Schwierigste,weil ich nicht weiß wie ich das mit deinem Code umsetzen soll.
In der Userform gibt es nicht nur ein Zielimage sondern insgesamt 22,davon sind 11 ausgeblendet.
die 11 eingeblendeten Images gehen von Image40 bis Image50,die ausgeblendeten fangen dann bei Image51 an.
Mit einem doppelklick auf eines der eingeblendeten Images geht dann das Parallelimage dazu auf.
Jetzt soll es so sein das jedes der 22 Images ein Zielimage sein kann.
Soll heißen,wenn ich unten ein Image ziehe soll es auf dem ausgewählten Zielimage landen
Hast du auch dazu eine Lösung
gruss Ivonne

Anzeige
AW: Drag and Drop in Userform
06.06.2014 08:41:22
Nepumuk
Hallo,
1. Was hat es mit den Parallelimage auf sich? Für die Zieleimages brauchen wir eine eigene Klasse.
2. Den ControlTipText können wir so übergeben (nur als Beispiel denn in der Klasse läuft das ein bisschen anders):
Private Sub Image0_BeforeDropOrPaste( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Action As MSForms.fmAction, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, _
        ByVal Y As Single, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    With Controls(Data.GetText)
        
        Set Image0.Picture = .Picture
        
        Image0.ControlTipText = .ControlTipText 'Tiptext übergeben
        
        Repaint
        
    End With
End Sub

Gruß
Nepumuk

Anzeige
AW: Drag and Drop in Userform
06.06.2014 11:51:03
Ivonne
Hi Nepumuk,
es geht darum, das Grafiken manchmal umgehängt werden, es soll aber auch festgehalten werden was vorher da war.
Deshalb bei Bedarf per Doppelklick das Parallelimage einblenden, so das dies mit einer Grafik belegt werden kann.
Man kann dann sehen was an dieser Position vorher und aktuell dort ist.
gruss Ivonne

AW: Drag and Drop in Userform
06.06.2014 14:27:13
Nepumuk
Hallo,
soll das "Parallel-Image" beim einblenden das Image aus seinem Parent übernehmen? Und beim Ausblenden ?
Gruß
Nepumuk

AW: Drag and Drop in Userform
06.06.2014 15:28:35
Ivonne
Hi Nepumuk,
nein es soll per doppelklick eingeblendet werden.
Das einfügen der Grafik soll dann wie bei den anderen per Drag and Drop erfolgen.
Jede Grafik aus den per Code erstellten Image(30st) soll in jedes feste Image(22st) per Drag and Drop einfügbar sein.
Besonderheit halt eben nur das von den 22 nur 11 sichtbar sind und die anderen bei Bedarf eingeblendet werden.
Hoffe habe es jetzt genau beschrieben.
gruss Ivonne

Anzeige
AW: Drag and Drop in Userform
06.06.2014 21:52:08
Nepumuk
Hallo,
dann teste mal:
' **********************************************************************
' Modul: clsSourceImage Typ: Klassenmodul
' **********************************************************************

Option Explicit

Private WithEvents mobjImage As MSForms.Image

Private Sub Class_Terminate()
    Set Image = Nothing
End Sub

Friend Property Get Image() As MSForms.Image
    Set Image = mobjImage
End Property

Friend Property Set Image(ByRef probjImage As MSForms.Image)
    Set mobjImage = probjImage
End Property

Private Sub mobjImage_BeforeDragOver( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, _
        ByVal Y As Single, _
        ByVal DragState As MSForms.fmDragState, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    Cancel = True
    Effect = fmDropEffectCopy
    
End Sub

Private Sub mobjImage_MouseDown( _
        ByVal Button As Integer, _
        ByVal Shift As Integer, _
        ByVal X As Single, _
        ByVal Y As Single)

    
    Dim objDataObject As DataObject
    
    If Button = 1 Then
        
        Set objDataObject = New DataObject
        Call objDataObject.SetText(Text:=Image.Name)
        Call objDataObject.StartDrag(OKEffect:=fmDropEffectCopy)
        Set objDataObject = Nothing
        
    End If
End Sub

' **********************************************************************
' Modul: clsTargetImage Typ: Klassenmodul
' **********************************************************************

Option Explicit

Private WithEvents mobjImage As MSForms.Image

Private mobjChildImage As MSForms.Image
Private mobjUserForm As Object

Private Sub Class_Terminate()
    Set Image = Nothing
    Set ChildImage = Nothing
    Set UserForm = Nothing
End Sub

Friend Property Get Image() As MSForms.Image
    Set Image = mobjImage
End Property

Friend Property Set Image(ByRef probjImage As MSForms.Image)
    Set mobjImage = probjImage
End Property

Friend Property Get ChildImage() As MSForms.Image
    Set ChildImage = mobjChildImage
End Property

Friend Property Set ChildImage(ByRef probjChildImage As MSForms.Image)
    Set mobjChildImage = probjChildImage
End Property

Friend Property Get UserForm() As Object
    Set UserForm = mobjUserForm
End Property

Friend Property Set UserForm(ByRef probjUserForm As Object)
    Set mobjUserForm = probjUserForm
End Property

Private Sub mobjImage_BeforeDragOver( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, _
        ByVal Y As Single, _
        ByVal DragState As MSForms.fmDragState, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    Cancel = True
    Effect = fmDropEffectCopy
    
End Sub

Private Sub mobjImage_BeforeDropOrPaste( _
        ByVal Cancel As MSForms.ReturnBoolean, _
        ByVal Action As MSForms.fmAction, _
        ByVal Data As MSForms.DataObject, _
        ByVal X As Single, _
        ByVal Y As Single, _
        ByVal Effect As MSForms.ReturnEffect, _
        ByVal Shift As Integer)

    
    With UserForm.Controls(Data.GetText)
        Set Image.Picture = .Picture
        Image.ControlTipText = .ControlTipText
    End With
    
    UserForm.Repaint
    
End Sub

Private Sub mobjImage_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    
    'Doppelklick zm ein- und ausblenden der Childimages
    
    If Not ChildImage Is Nothing Then
        
        With ChildImage
            
            'Bild beim Ausblenden löschen
            ' If .Visible Then
            '
            ' Set .Picture = Nothing
            ' .ControlTipText = vbNullString
            '
            ' End If
            
            .Visible = Not .Visible
            
        End With
    End If
End Sub

Private Sub mobjImage_MouseUp( _
        ByVal Button As Integer, _
        ByVal Shift As Integer, _
        ByVal X As Single, _
        ByVal Y As Single)

    
    'Rechtsklick zum Löschen des Bildes
    
    If Button = 2 Then
        
        With Image
            Set .Picture = Nothing
            .ControlTipText = vbNullString
        End With
        
        UserForm.Repaint
        
    End If
End Sub

' **********************************************************************
' Modul: UserForm1 Typ: Userform
' **********************************************************************

Option Explicit

Private mobjSourceImageClassCollection As Collection
Private mobjTargetImageClassCollection As Collection

Private Sub UserForm_Activate()
    
    Dim ialngIndex As Long
    Dim sngLeft As Single
    Dim avntValues As Variant
    Dim objImage As MSForms.Image
    Dim objSourceImageClass As clsSourceImage
    Dim objTargetImageClass As clsTargetImage
    
    With Worksheets("Tabelle2")
        avntValues = .Range("A1:E30").Value2
    End With
    
    Set mobjSourceImageClassCollection = New Collection
    
    For ialngIndex = 1 To 30
        
        Set objImage = Controls.Add(bstrProgID:="Forms.Image.1", _
            Name:="Image" & CStr(ialngIndex))
        
        With objImage
            .Left = sngLeft
            .Top = 389
            .Width = 30
            .Height = 40
            .PictureSizeMode = fmPictureSizeModeStretch
            Set .Picture = LoadPicture(avntValues(ialngIndex, 1))
            .ControlTipText = avntValues(ialngIndex, 5)
        End With
        
        Set objSourceImageClass = New clsSourceImage
        
        Set objSourceImageClass.Image = objImage
        
        Call mobjSourceImageClassCollection.Add(Item:=objSourceImageClass)
        
        sngLeft = sngLeft + 30
        
    Next
    
    Set mobjTargetImageClassCollection = New Collection
    
    For ialngIndex = 40 To 50
        
        Set objTargetImageClass = New clsTargetImage
        
        With objTargetImageClass
            Set .Image = Controls("Image" & CStr(ialngIndex))
            Set .ChildImage = Controls("Image" & CStr(ialngIndex + 11))
            Set .UserForm = Me
        End With
        
        Call mobjTargetImageClassCollection.Add(Item:=objTargetImageClass)
        
    Next
    
    For ialngIndex = 51 To 61
        
        Set objTargetImageClass = New clsTargetImage
        
        With objTargetImageClass
            Set .Image = Controls("Image" & CStr(ialngIndex))
            Set .UserForm = Me
        End With
        
        Call mobjTargetImageClassCollection.Add(Item:=objTargetImageClass)
        
    Next
    
    Set objTargetImageClass = Nothing
    Set objSourceImageClass = Nothing
    Set objImage = Nothing
    
End Sub

Private Sub UserForm_Terminate()
    Set mobjSourceImageClassCollection = Nothing
    Set mobjTargetImageClassCollection = Nothing
End Sub

Gruß
Nepumuk
Anzeige

92 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Links zu Excel-Dialogen

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige