Results 1 to 9 of 9

Thread: listbox + hidden value?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    listbox + hidden value?

    Howdy people,

    I know there is a code to do this, but I forgot what it is...


    It was something where i can listbox.items.add("Visible Value in LB", "Hidden Value") or vice versa...

    Reasoning:

    Trying to make simple chat, with multiple rooms...

    When User 1 connects, his id is 1, User 2 connects with his id of 2.

    When User 1 leaves the room, i need the user 2 client to remove 'User 1'(id of 1).


    Thanks in advance.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listbox + hidden value?

    you'd use a custom listItem:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Structure listItem
    4.         Dim _display As String
    5.         Dim _hidden As String
    6.         Public Sub New(ByVal display As String, ByVal hidden As String)
    7.             Me._display = display
    8.             Me._hidden = hidden
    9.         End Sub
    10.         Public Overrides Function ToString() As String
    11.             Return Me._display
    12.         End Function
    13.     End Structure
    14.  
    15.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    16.         Dim strings() As String = {"one", "two", "three", "four", "five"}
    17.         For x As Integer = 1 To 5
    18.             ListBox1.Items.Add(New listItem(strings(x - 1), x.ToString))
    19.         Next
    20.     End Sub
    21.  
    22.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    23.         MsgBox(DirectCast(ListBox1.SelectedItem, listItem)._hidden)
    24.     End Sub
    25.  
    26. End Class

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: listbox + hidden value?

    So, in your example,

    (display = hidden)
    One = 1
    Two = 2
    Three = 3
    Four = 4
    Five = 5

    How would I remove the ID of 5?(the hidden value)

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listbox + hidden value?

    Code:
    Public Class Form1
    
        Private Structure listItem
            Dim _display As String
            Dim _hidden As String
            Public Sub New(ByVal display As String, ByVal hidden As String)
                Me._display = display
                Me._hidden = hidden
            End Sub
            Public Overrides Function ToString() As String
                Return Me._display
            End Function
        End Structure
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim strings() As String = {"one", "two", "three", "four", "five"}
            For x As Integer = 1 To 5
                ListBox1.Items.Add(New listItem(strings(x - 1), x.ToString))
            Next
            'to remove the hidden value of the fifth item
            Dim l As listItem = DirectCast(ListBox1.Items(4), listItem)
            l._hidden = ""
            ListBox1.Items(4) = l
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
            MsgBox(DirectCast(ListBox1.SelectedItem, listItem)._hidden)
        End Sub
    
    End Class

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: listbox + hidden value?

    Not quite what I meant.

    In short..a list box of:

    (Displayed Username = Hidden UserID)
    TCarter = 283
    .paul. = 193
    Jason = 2293
    Joe = 289732
    Ryan = 892732
    Adam = 2987392873
    Pearl = 111111
    Patrick = 289732111


    Ok, so.

    When I want to remove 111111 (which is Pearl)

    I want to do whatever.remove("111111") (not necessarily that command..just an example)

    It will remove 'Pearl' from the list. Not just remove the '111111'.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listbox + hidden value?

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Structure listItem
    4.         Dim _display As String
    5.         Dim _hidden As String
    6.         Public Sub New(ByVal display As String, ByVal hidden As String)
    7.             Me._display = display
    8.             Me._hidden = hidden
    9.         End Sub
    10.         Public Overrides Function ToString() As String
    11.             Return Me._display
    12.         End Function
    13.     End Structure
    14.  
    15.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    16.         Dim strings(,) As String = {{"TCarter", "283"}, {".paul.", "193"}, {"Jason", "2293"}, {"Joe", "289732"}, {"Ryan", "892732"}, {"Adam", "2987392873"}, {"Pearl", "111111"}, {"Patrick", "289732111"}}
    17.         For x As Integer = 1 To 8
    18.             ListBox1.Items.Add(New listItem(strings(x - 1, 0), strings(x - 1, 1)))
    19.         Next
    20.     End Sub
    21.  
    22.     Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    23.         MsgBox(DirectCast(ListBox1.SelectedItem, listItem)._hidden)
    24.     End Sub
    25.  
    26.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    27.         'removeByName
    28.         Dim [name] As String = InputBox("enter name to delete")
    29.         Dim item = (From li In ListBox1.Items.Cast(Of listItem)().ToArray _
    30.                                Where li._display = [name] _
    31.                                Select li)
    32.         If item.Count > 0 Then ListBox1.Items.Remove(item.First)
    33.  
    34.     End Sub
    35.  
    36.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    37.         'removeByID
    38.         Dim id As String = InputBox("enter ID to delete")
    39.         Dim item = (From li In ListBox1.Items.Cast(Of listItem)().ToArray _
    40.                                Where li._hidden = id _
    41.                                Select li)
    42.         If item.Count > 0 Then ListBox1.Items.Remove(item.First)
    43.     End Sub
    44. End Class

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: listbox + hidden value?

    Thank you oh so much paul.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Re: listbox + hidden value?

    I truly do appreciate all the help, but would like to ask of 1 more thing.


    How can I find the display string, associated with the hidden string?

    Example: 'whatever("idgoeshere")' returns: the displaystring here.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: listbox + hidden value?

    here's how to do it. it's not a function but you get the idea...

    vb Code:
    1. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    2.     'getNameFromID
    3.     Dim id As String = InputBox("enter ID to find")
    4.     Dim item = (From li In ListBox1.Items.Cast(Of listItem)().ToArray _
    5.                            Where li._hidden = id _
    6.                            Select li._display)
    7.     If item.Count > 0 Then MsgBox(item.First)
    8. End Sub

Posting Permissions

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



Click Here to Expand Forum to Full Width