tags:

views:

21

answers:

1

I want to manually add two items as "Active" and "Inactive" in a ListBox. When the user selects "Active", i want to get the value "A" and when "Inactive" is selected, i want to get "I".

How do i do this in VB.NET.

Regards, George

+1  A: 

Are you using .NET 4? If so, the simplest solution is probably to use Tuple(Of String, String). Create a tuple of ("Active", "A") and another of ("Inactive", "I") and add those to the listbox. Then set the listbox's DisplayMember property to "Item1" and ValueMember to "Item2".

Or you could do the same sort of thing with an anonymous type.

Jon Skeet
Dear Jon, thanks for the reply. Actually, i am having an invisible listbox with data populated from a stored procedure and i have populated it by setting the DataSource. Similarly, those items, i want it to get populated into the second list box by using items.add. The requirement is that i can use the second list box for removing items by clicking a button bcaause items cannot be removed from a list box to which data is populated using DataSource. Please advice.
George Trevour Dsouza
@George: I don't see how your extra description is relevant to the current text of your question, nor why you need an invisible listbox to start with. Please edit your question with much more context. (You haven't even said whether this is WPF, WinForms, ASP.NET etc.)
Jon Skeet
Dear Jon, am working with VB.NET [Windows Forms] and it is a desktop application. I have populated a list box using stored procedures and setting the datasource property of the list box to get the information returned from a stored procedure. No there is a button called "Delete" and when i select an item from the list box and click "Delete", the item should get deleted. I tried "ListBox1.Items.RemoveAt(ListBox1.SelectedIndex), but it returns an error. Now what i do is that, when the first list box gets populated, i want another one also to get populated using Items.Add, so i can delete it.
George Trevour Dsouza
@George: You still haven't said *why* you've got this hidden listbox... and please edit all this information into the question rather than using comments.
Jon Skeet
Removing Items from a ListBox after setting the DataSource
George Trevour Dsouza
Dear Jon, i have posted a query about the same just before this question as "Removing Items from a ListBox after setting the DataSource" and there is no reply from anyone to that question. Can you please check that and help me with an answer.
George Trevour Dsouza