Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox problem

Status
Not open for further replies.

BadCook

Technical User
Sep 7, 2009
71
US
I have a series of labels each holding data that I would like to store in a List by the user hitting a "Save" button after highlighting a label.
How do I prevent the user from repeatingly entering the same label information by hitting "Save" multiple times. I would like the label to retain the information.
I suppose a "RemoveItem" method is involved, but I'm having trouble coding it.
Any help would be appreciated.
 
You may want to capture and track what values the user is entering. A quick sample with some minor error handling.

Code:
Option Explicit
Dim dict As New Dictionary

Private Sub Command1_Click()
    If dict.Exists(Trim$(Text1.Text)) And Len(Trim$(Text1.Text)) > 0 Then
        MsgBox "You must enter a unique value to be added to the list", vbInformation + vbOKOnly
        With Text1
            .SetFocus
            .SelStart = 0
            .SelLength = Len(.Text)
        End With
        Exit Sub
    ElseIf Len(Trim$(Text1.Text)) > 0 Then
        dict.Add Trim$(Text1.Text), Trim$(Text1.Text)
        List1.AddItem Trim$(Text1.Text)
        With Text1
            .SetFocus
            .Text = ""
        End With
    Else
        MsgBox "Please enter information to be entered!", vbInformation + vbOKOnly
        Text1.SetFocus
    End If
End Sub

Swi
 
Alternatively you can verify whether an item is already in the Listbox by some judicious and fairly simple use of the Windows API. I leave it as an exercise for the reader on how to modify this appropriately:

Code:
[blue]Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const LB_FINDSTRING = &H18F

Private Sub Command1_Click()
    Dim strExample As String
    strExample = "Mike"
    
    List1.AddItem "Soup"
    List1.AddItem "dragon"
    List1.AddItem strExample
    
    If SendMessage(List1.hwnd, LB_FINDSTRING, 0&, strExample) Then MsgBox "Found"
End Sub[/blue]
 
I solved this problem by adding the index idenemtity of the label (or text box) to the message of that source as a preamble. So when the user enters the information the program first splits the message and gets the preamble. It then scans the saved entrants in the list box by spliting each stored message and comparing the stored preamble with the new. if it gets a match it removes that entry and enters the new message, preamble and all.
Thus a new updated message replases the old, wich is what I want. The user eepeatedly entering the same message has no effect
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top