Wunderbar! Das Ganze ist jetzt noch übersichtlicher und funktioniert einwandfrei. Hier der zum "nachbasteln"
Folgende Userform besteht aus einem CommandButton= Select_btn einer Combobox=SelectionBox, einem Bild=Logo und einem CommandButton=Back_btn.
Die Klasse ListEntry erzeugt einen Button mit einem Bild (hier ein Minuszeichen) und ein Label. Das Label speichert den Eintrag der in der Combobox ausgewählt wurde mittels des Select Buttons. Klickt man Select wird also der aktuell ausgewählte Eintrag als Listentry.lbl.caption gespeichert. Wenn der Button des Entries gedrückt wird verschwindet der Eintrag, die Einträge werden mit einem aktualisierten Index und Namen versehen. Dazu wird noch ein Modul namens UI verwendet.
Hier der Code
Klasse: ListEntry
Public WithEvents btn As MSForms.CommandButton
Public Index As Integer
Public WithEvents lbl As MSForms.Label
Private Sub btn_Click()
frmMaterial.Height = frmMaterial.Height - 40
frmMaterial.Logo.Top = frmMaterial.Logo.Top - 40
frmMaterial.Backbtn.Top = frmMaterial.Backbtn.Top - 40
frmMaterial.GO_btn.Top = frmMaterial.GO_btn.Top - 40
frmMaterial.Controls.Remove ("Deselect" & Index)
frmMaterial.Controls.Remove ("Selection" & Index)
frmMaterial.lstCol.Remove (Index)
Call UI.Reindex
Call UI.Respace
If frmMaterial.lstCol.Count = 0 Then
frmMaterial.GO_btn.Enabled = False
End If
End Sub
Userform frmMaterials:
Option Explicit
'This is written by Lucas Raphael Pianegonda @ EMS Chemie. Since the employment contract does not treat copyright, the copyright
'of this code is owned by Lucas Meier. The code can be used and modified by others in or outside the company. However any licencing,
'commerciallization of the code it self and not it's produced outputs can only occure with the explicit permission of Lucas Raphael Pianegonda.
'This userform is meant to select different materials to compare
Public SP As Boolean ' Singelpoint
Public MP As Boolean ' Multipoint
Public DS As Boolean ' Datasheets
Public SC As Boolean ' Scatterplots
Public lstCol As New Collection
Private Sub GO_btn_Click()
Me.Hide
If Not (frmUI.MP Xor frmUI.SP Xor frmUI.DS Xor frmUI.SC) Then
GoTo Fehler
End If
If frmUI.MP Then
frmFilter.Show
Me.Hide
ElseIf frmUI.SP Then
frmFilter.Show
Me.Hide
ElseIf frmUI.DS Then
DataSheets.DataSheets_show
Me.Hide
ElseIf frmUI.SC Then
MsgBox "Scatterplot was activated, but this makes no sense here."
Me.Hide
End If
Exit Sub
Fehler:
MsgBox "There is not exactly one true trigger there is something wrong. Either all of them are False or more then one are True."
End Sub
Private Sub UserForm_Initialize()
Set lstCol = New Collection
End Sub
Private Sub Select_btn_Click()
Dim Entry As New ListEntry
lstCol.Add Entry
' Adjust Userform
Me.Height = Me.Height + 40
Me.Logo.Top = Me.Logo.Top + 40
Me.GO_btn.Top = Me.GO_btn.Top + 40
Me.Backbtn.Top = Me.Backbtn.Top + 40
' Text Label
Set Entry.lbl = Me.Controls.Add("Forms.Label.1", "Selection" & lstCol.Count, True)
With Entry.lbl.Font
.name = "Arial"
.Size = 16
.Bold = True
End With
Entry.lbl.Caption = "" & SelectionBox.Value
Entry.lbl.Top = Me.SelectionBox.Top + 40 * (lstCol.Count) + 7
Entry.lbl.Left = Me.SelectionBox.Left
Entry.lbl.Width = Me.SelectionBox.Width
Entry.lbl.Height = Me.SelectionBox.Height
' Entry Button
Set Entry.btn = Me.Controls.Add("Forms.CommandButton.1", "Deselect" & lstCol.Count, True)
Entry.Index = lstCol.Count
With Entry.btn
.BackColor = &HFFFFFF
.ForeColor = &H0&
.Picture = LoadPicture("V:\GR-QKMP\AUFTRAG\Datenbanktool\Datenbanktool_VBA_Project\Sourcedata_and_Modules\Icons\minus.jpg")
.PicturePosition = 12
End With
With Entry.btn.Font
.name = "Arial"
.Size = 16
.Bold = True
End With
Entry.btn.Top = Me.Select_btn.Top + 40 * (lstCol.Count)
Entry.btn.Left = Me.Select_btn.Left - Me.Select_btn.Height + Me.Select_btn.Width
Entry.btn.Width = Me.Select_btn.Height
Entry.btn.Height = Me.Select_btn.Height
Entry.btn.TakeFocusOnClick = False
frmMaterial.GO_btn.Enabled = True
End Sub
Private Sub Backbtn_Click()
frmUI.MP = False
frmUI.SP = False
frmUI.DS = False
frmUI.SC = False
frmMaterial.Hide
frmUI.Show
End Sub
Code UI Modul:
Option Explicit
Public Sub Respace()
Dim Entry As ListEntry
Dim Index As Integer
Index = 1
For Each Entry In frmMaterial.lstCol
Entry.btn.Top = frmMaterial.Select_btn.Top + Index * 40
Entry.lbl.Top = frmMaterial.SelectionBox.Top + Index * 40
Index = Index + 1
Next Entry
End Sub
Sub Reindex()
Dim Entry As ListEntry
Dim Idx As Integer
Dim lbl As MSForms.Label
Idx = 1
For Each Entry In frmMaterial.lstCol
Entry.Index = Idx
Entry.btn.name = "Deselect" & Idx
Entry.lbl.name = "Selection" & Idx
Idx = Idx + 1
Next Entry
End Sub
Background Info: frmUI als Hauptmenü und frmFilter als weiteres Untermenü werden im Code verwendet. Das Hauptmenü hat vier Buttons einer für SinglePoint Daten =SP, einer für Multipoint Daten=MP, eine für Data Sheets =DS und eine für einen Scatterplot =SC. Die jeweiligen Booleans erlauben mir nach dem Aufruf der letzten Filterform festzustellen welcher Button anfangs gedrückt wurde.
Entry.btn.Top = Me.Select_btn.Top + 40 * (lstCol.Count)
Entry.btn.Left = Me.Select_btn.Left - Me.Select_btn.Height + Me.Select_btn.Width
Entry.btn.Width = Me.Select_btn.Height
Entry.btn.Height = Me.Select_btn.Height
Entry.btn.TakeFocusOnClick = False
frmMaterial.GO_btn.Enabled = True
End Sub
Private Sub Backbtn_Click()
frmUI.MP = False
frmUI.SP = False
frmUI.DS = False
frmUI.SC = False
frmMaterial.Hide
frmUI.Show
End Sub
|