Consulting

Results 1 to 3 of 3

Thread: Manipulating multi line TB in UserForm - add and remove specific lines

  1. #1
    VBAX Regular
    Joined
    Jun 2016
    Posts
    45
    Location

    Manipulating multi line TB in UserForm - add and remove specific lines

    Hello,

    i am currently working on a userform which allows the user to add specific selections (text from listboxes) to a multiline textbox.
    Now i am trying to give the user the option to remove certain lines of text from this textbox in case that a false selection has been added to the textbox.

    This is what i currently have:

    Populating the listboxes: (don't mind the variables, i just add this here, so that you can see where content for the multiline textbox is coming from)

        
    Private Sub UserForm_Initialize()
    
        With ListBox1
            .AddItem "Gehäusedichtung"
            .AddItem "O-Ring Gehäusedichtung"
            .AddItem "O-Ring Stoßfangdichtung"
            .AddItem "Ventileinsatzdichtung"
            .AddItem "Filterkäfigdichtung"
            .AddItem "O-Ring Filterkäfigdichtung"
            .AddItem "Überdruck-Ventiltellerdichtung"
            .AddItem "Unterdruck-Ventiltellerdichtung"
            .AddItem "Ablassschraubendichtung"
        End With
    
        With ListBox2
            .AddItem "Flammensicherung"
            .AddItem "Kondensatablaufsicherung"
            .AddItem "Flammenfilterscheibe (L)"
            .AddItem "Flammenfilterscheibe (R)"
            .AddItem "Flammenfilterscheibe (G)"
        End With
    
        With ListBox3
            .AddItem "Überdruck-Ventilteller"
            .AddItem "Unterdruck-Ventilteller"
        End With
        
        If ActiveDocument.FormFields("TypAdd1").Range = "/" Then
            appNameTBContent = ActiveDocument.FormFields("Hersteller").Range  & "®" & " " & ActiveDocument.FormFields("Typ").Range &  "-" & ActiveDocument.FormFields("TypAdd2").Range
        Else
            appNameTBContent = ActiveDocument.FormFields("Hersteller").Range  & "®" & " " & ActiveDocument.FormFields("Typ").Range &  "-" & ActiveDocument.FormFields("TypAdd1").Range & "-" &  ActiveDocument.FormFields("TypAdd2").Range
        End If
        appNameTB.Text = appNameTBContent
    
    End Sub
    I have two CommandButtons in the UserForm. One for adding a selected Listbox item to the Textbox (cmdAddLB1) and one for removing a selected Listbox item from the Textbox (cmdRemoveLB1).
    Selecting an item from the ListBox and adding this selection to the textbox by clicking the cmdAddLB1 CommandButton works just fine.
    But i am a little bit confused on how i would go about removing selected Listbox items from the Textbox when the cmdRemoveLB1 CommandButton is clicked.

    To add items to the Textbox i wrote the following code:

    Private Sub cmdAddLB1_Click()
    Dim ListBoxItem As Variant
    Dim gasketPreview0, gasketPreview1, gasketPreview2, gasketPreview3,  gasketPreview4, gasketPreview5, gasketPreview6, gasketPreview7,  gasketPreview8 As String
    
    'Just comments to see the content of each ListBox index
    'ListBox Index 0 = "Gehäusedichtung"
    'ListBox Index 1 = "O-Ring Gehäusedichtung"
    'ListBox Index 2 = "O-Ring Stoßfangdichtung"
    'ListBox Index 3 = "Ventileinsatzdichtung"
    'ListBox Index 4 = "Filterkäfigdichtung"
    'ListBox Index 5 = "O-Ring Filterkäfigdichtung"
    'ListBox Index 6 = "Überdruck-Ventiltellerdichtung"
    'ListBox Index 7 = "Unterdruck-Ventiltellerdichtung"
    'ListBox Index 8 = "Ablassschraubendichtung"
        
        Select Case ListBoxItem
        Case ListBox1.Value = 0
        If SpinButtonLB1.Value = 0 Then
            gasketPreview0 = "• " & appNameTB.Text & " " &  ActiveDocument.FormFields("GDTNG").Range & " " & ListBox1.Value  & " erneuert."
            If contentPreview.Text = "" Then
            contentPreview.Text = gasketPreview0
            ElseIf Not contentPreview.Text = "" Then
            contentPreview.Text = contentPreview.Text & vbCrLf & gasketPreview0
            End If
        ElseIf SpinButtonLB1.Value > 0 Then
            gasketPreview0 = "• " & counter1.Text & "x " &  appNameTB.Text & " " & ActiveDocument.FormFields("GDTNG").Range  & " " & ListBox1.Value & " erneuert."
            If contentPreview.Text = "" Then
            contentPreview.Text = gasketPreview0
            ElseIf Not contentPreview.Text = "" Then
            contentPreview.Text = contentPreview.Text & vbCrLf & gasketPreview0
            End If
        End If
    
        Case ListBox1.Value = 1
            contentPreview.Text = "• " & appNameTB.Text & " " &  ActiveDocument.FormFields("GDTNG").Range & " " & ListBox1.Value  & " erneuert."
        Case ListBox1.Value = 2
            contentPreview.Text = "• " & appNameTB.Text & " " &  ActiveDocument.FormFields("GDTNG").Range & " " & ListBox1.Value  & " erneuert."
        Case ListBox1.Value = 3
            contentPreview.Text = "• " & appNameTB.Text & " " &  ActiveDocument.FormFields("GDTNG").Range & " " & ListBox1.Value  & " erneuert."
        Case ListBox1.Value = 4
            contentPreview.Text = "• " & appNameTB.Text & " " &  ActiveDocument.FormFields("GDTNG").Range & " " & ListBox1.Value  & " erneuert."
        Case ListBox1.Value = 5
            contentPreview.Text = "• " & appNameTB.Text & " " &  ActiveDocument.FormFields("GDTNG").Range & " " & ListBox1.Value  & " erneuert."
        Case ListBox1.Value = 6
            contentPreview.Text = "• " & appNameTB.Text & " " &  ActiveDocument.FormFields("PVDTNG").Range & " " & ListBox1.Value  & " erneuert."
        Case ListBox1.Value = 7
            contentPreview.Text = "• " & appNameTB.Text & " " &  ActiveDocument.FormFields("VVDTNG").Range & " " & ListBox1.Value  & " erneuert."
        Case ListBox1.Value = 8
            contentPreview.Text = "• " & appNameTB.Text & " " & ListBox1.Value & " erneuert."
        Case Else
            MsgBox "Es wurde nichts ausgewählt!" & vbCr & _
            "Wähle einen Listeneintrag um diesen zu übertragen!"
        End Select
    End Sub

    Private Sub cmdRemoveLB1_Click()
    ' No working code so far =(
    End Sub
    I dont really know how the cmdRemoveLB1_Click() procedure needs to be set up in order for this to work.
    Is this only possible with an array?
    Declaring the contentPreview TextBox as an array,
    Clicking cmdAddLB1 would add the selected ListBox item to that array.
    Clicking cmdRemoveLB1 would remove the selected ListBox from that array?

    I have attached a screenshot of the UserForm to illustrate my problem.
    I also attached a simplified testDocument to take a look at if someone is interested to help.

    Attachment 25289


    If someone could help me out here it would be much appreciated.

    best regards

    Manuel
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    It is not clear (could be a language issue) why you need the remove button. Make the listbox multiselect, rename the Add button to Add/Update, delete the Remove button and then use the add button to preview selected items in the list.

    Private Sub cmdAddLB1_Click()Dim i As Integer
        contentPreview.Text = ""
        For i = 0 To ListBox1.ListCount - 1
            If ListBox1.Selected(i) Then
                If contentPreview.Text = "" Then
                    contentPreview.Text = ListBox1.List(i)
                Else
                    contentPreview.Text = contentPreview.Text & vbCrLf & ListBox1.List(i)
                End If
            End If
        Next i
        For i = 0 To ListBox1.ListCount - 1
            ListBox1.Selected(i) = False
        Next i
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Jun 2016
    Posts
    45
    Location
    Hello,

    thanks for your reply and attempt to help me out.

    Well, the remove button is obviously not needed at all, thanks for pointing that out.

    Your code is working so far, however it is not working as i intend it to.
    Whenever an item from the listbox is selected and added to the contentPreview textbox, i want it to display a much longer sentence, as the words in the listbox are just placeholders for what actually should be displayed in the textbox.
    I dont want to have the whole sentence displayed as a listbox item, so i would need the contentPreview to change to a certain sentence or sentences, depending on what items are selected.

    For example:

    • The first item in the listbox says "Housing Gasket".
    • The sentence that gets written in the contentPreview Textbox should then be something like "[manufacturer] [type] [size] Housing Gasket replaced." (the variables in the square brackets are coming from selections the user made in other parts of the document, i just showcase this here, so that you can see why i can't display the whole text as a listbox item, because they are pretty long and would not fit in the listbox.)
    • So if the user selects multiple items from the listbox, each item should refer to a specific sentence, which should then be displayed in the textbox. I hope that makes sense.

    Is there a proper way of doing this?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •