I have a listbox that I can select multiple items from, when I do that I want to remove selected items, but I can't quite get it to work. Can someone please post a example code?
Since you use the vb insted of C# I think you got this problem because language has several implementation. above my code is woked properly in C#. Yes I test it with vb it get the error that u mentioned. so try this one
Dim i As
Integer
While ListBox1.Items.Count > i
If ListBox1.Items(i).Selected =
True Then
ListBox1.Items.RemoveAt(i)
i = i - 1
End If
i = i + 1
End While
Chandana
Marked as answer by Ivan Xin - MSFT on May 05, 2008 03:27 AM
modig
Member
475 Points
420 Posts
remove selected items in listbox control
May 02, 2008 06:00 AM|LINK
Hi
I have a listbox that I can select multiple items from, when I do that I want to remove selected items, but I can't quite get it to work. Can someone please post a example code?
yeotumitsu@s...
Contributor
4907 Points
836 Posts
Re: remove selected items in listbox control
May 02, 2008 06:10 AM|LINK
-Manas
=======================================
If this post is useful to you, please mark it as answer.
sivakl_2001
Contributor
2849 Points
1069 Posts
Re: remove selected items in listbox control
May 02, 2008 06:18 AM|LINK
hi modig if u want to delete selected items on listbox try the following codes
while (listBox1.SelectedItems.Count != 0){
listBox1.Items.Remove(listBox1.SelectedItems[0]);
}
chandana
Participant
1356 Points
378 Posts
Re: remove selected items in listbox control
May 02, 2008 06:34 AM|LINK
when you suppose to remove the item can u mention it clearly your requirment?
try with this code but the way it application is differ with your answer of above question
for (int i = 0; i < ListBox1.Items.Count; i++){
if (ListBox1.Items[i].Selected){
ListBox1.Items.RemoveAt(i);
i = i - 1;
}
}
modig
Member
475 Points
420 Posts
Re: remove selected items in listbox control
May 02, 2008 07:20 AM|LINK
Hi
Thanks for the code but I'm sorry to say none of them work....
1. Selcount is not a member of Web.UI.WebControls.Lisbox
2. SelectedItems is not a member of Web.UI.WebControls.Lisbox
3. When I run the third example from chandana, I get a index out of range error.
Regards
chandana
Participant
1356 Points
378 Posts
Re: remove selected items in listbox control
May 02, 2008 07:48 AM|LINK
can u show me how to insert the item to list box. because i test my code; it works.
sivakl_2001
Contributor
2849 Points
1069 Posts
Re: remove selected items in listbox control
May 02, 2008 07:57 AM|LINK
hi modig this simple code help u
while (ListBox1.SelectedIndex != -1){
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex);
}
modig
Member
475 Points
420 Posts
Re: remove selected items in listbox control
May 02, 2008 07:57 AM|LINK
I have 2 textboxes on my page and when I click the button1 the text from thoose textboxes are inserted..
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.ClicksLine4.Items.Add(
New ListItem(sLine2.Text.ToString, (sLine2.Text.ToString))) 'textbox1 sLine4.Items.Add(New ListItem(sLine3.Text.ToString, sLine3.Text.ToString)) 'textbox2sLine4.Items.Add(
New ListItem("---", "---")) End Subchandana
Participant
1356 Points
378 Posts
Re: remove selected items in listbox control
May 02, 2008 09:17 AM|LINK
Since you use the vb insted of C# I think you got this problem because language has several implementation. above my code is woked properly in C#. Yes I test it with vb it get the error that u mentioned. so try this one
Dim i As Integer While ListBox1.Items.Count > i If ListBox1.Items(i).Selected = True ThenListBox1.Items.RemoveAt(i)
i = i - 1
End Ifi = i + 1
End Whilesivakl_2001
Contributor
2849 Points
1069 Posts
Re: remove selected items in listbox control
May 02, 2008 09:55 AM|LINK
wat abt ur previous question wat is ur new question i can't understand the question