|
-
Aug 7th, 2010, 03:24 PM
#1
Thread Starter
Fanatic Member
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.
-
Aug 7th, 2010, 04:00 PM
#2
Re: listbox + hidden value?
you'd use a custom listItem:
vb 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
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2010, 04:37 PM
#3
Thread Starter
Fanatic Member
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)
-
Aug 7th, 2010, 04:48 PM
#4
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2010, 04:54 PM
#5
Thread Starter
Fanatic Member
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'.
-
Aug 7th, 2010, 05:18 PM
#6
Re: listbox + hidden value?
vb 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 = {{"TCarter", "283"}, {".paul.", "193"}, {"Jason", "2293"}, {"Joe", "289732"}, {"Ryan", "892732"}, {"Adam", "2987392873"}, {"Pearl", "111111"}, {"Patrick", "289732111"}}
For x As Integer = 1 To 8
ListBox1.Items.Add(New listItem(strings(x - 1, 0), strings(x - 1, 1)))
Next
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
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'removeByName
Dim [name] As String = InputBox("enter name to delete")
Dim item = (From li In ListBox1.Items.Cast(Of listItem)().ToArray _
Where li._display = [name] _
Select li)
If item.Count > 0 Then ListBox1.Items.Remove(item.First)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'removeByID
Dim id As String = InputBox("enter ID to delete")
Dim item = (From li In ListBox1.Items.Cast(Of listItem)().ToArray _
Where li._hidden = id _
Select li)
If item.Count > 0 Then ListBox1.Items.Remove(item.First)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 7th, 2010, 05:25 PM
#7
Thread Starter
Fanatic Member
Re: listbox + hidden value?
Thank you oh so much paul.
-
Aug 7th, 2010, 05:46 PM
#8
Thread Starter
Fanatic Member
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.
-
Aug 7th, 2010, 05:51 PM
#9
Re: listbox + hidden value?
here's how to do it. it's not a function but you get the idea...
vb Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'getNameFromID
Dim id As String = InputBox("enter ID to find")
Dim item = (From li In ListBox1.Items.Cast(Of listItem)().ToArray _
Where li._hidden = id _
Select li._display)
If item.Count > 0 Then MsgBox(item.First)
End Sub
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|