Lanc1988 Posted February 13, 2005 Posted February 13, 2005 I have a listbox in which users can add items to it.. i would like to have a button that lets them move the selected item either up or down. I called my checkedlistbox "listboxMiscGoals" Quote
Leaders snarfblam Posted February 13, 2005 Leaders Posted February 13, 2005 Assuming you are using vb... Sub MoveSelectedItemUp(ByVal Box As ListBox) Dim Index As Integer = Box.SelectedIndex 'Index of selected item Dim Swap As Object = Box.SelectedItem 'Selected Item If Not (Swap Is Nothing) Then 'If something is selected... Box.Items.RemoveAt(Index) 'Remove it Box.Items.Insert(Index - 1, Swap) 'Add it back in one spot up Box.SelectedItem = Swap 'Keep this item selected End If End Sub [/Code] If you are c# then... this is PROBABLY WRONG but you can get the jist of it [Code] void MoveSelectedItemUp(ListBox Box) { int Index = Box.SelectedIndex; //Selected Index object Swap = Box.SelectedItem; //Selected Item If (Index == -1) { //If something is selected... Box.Items.RemoveAt(Index); //Remove it Box.Items.Insert(Index - 1, Swap); //Add it back in one spot up Box.SelectedItem = Swap; //Keep this item selected } } [/Code] Quote [sIGPIC]e[/sIGPIC]
Lanc1988 Posted February 13, 2005 Author Posted February 13, 2005 yeah, i needed the vb one, thanks :) Quote
Lanc1988 Posted February 13, 2005 Author Posted February 13, 2005 im having a problem when its already at the top and they click to move it up.. i stopped the error from happening, but it is completely removing the goal. I tried adding Try, Catch, and End Try, but i dont know what to put under Catch to make it insert the item in the first place. Quote
Leaders snarfblam Posted February 13, 2005 Leaders Posted February 13, 2005 Ideally if the selected item is already at the top you want to disable the "move up" button since it cant be moved up. If you prefer not to, then replace the line "If Not (Swap Is Nothing) Then" with "If Index > 0 Then". This will only move items with an index 1 or higher (i.e. the second or higher item on the list). Quote [sIGPIC]e[/sIGPIC]
Lanc1988 Posted February 13, 2005 Author Posted February 13, 2005 ok, when i replaced it with If Index > 0 Then it works fine for the move up, but what would i replace the If Not (Swap is nothing) part with for my move down button? Quote
Leaders snarfblam Posted February 14, 2005 Leaders Posted February 14, 2005 Just as the "Index > 0" condition tests if the selected item's index is greater than that of the first item, the move down function should test if the selected index is less than that of the last item. Keep in mind that you also need to either test if the index = -1 or if Swap is Nothing, either of which will indicate that nothing is selected. So... here. Have some code. Sub MoveSelectedItemDown(ByVal Box As ListBox) Dim Index As Integer = Box.SelectedIndex 'Index of selected item Dim Swap As Object = Box.SelectedItem 'Selected Item If (Index <> -1) AndAlso (Index + 1 < Box.Items.Count) Then Box.Items.RemoveAt(Index) 'Remove it Box.Items.Insert(Index + 1, Swap) 'Add it back in one spot up Box.SelectedItem = Swap 'Keep this item selected End If End Sub Quote [sIGPIC]e[/sIGPIC]
janblo Posted February 3, 2009 Posted February 3, 2009 I've actually created an ordered listbox in C# here : http://blogs.gaiaware.net/post/Building-an-Ordered-Ajax-ListControl-in-Gaia-Ajax-using-C.aspx I've also used Gaia Ajax to automatically ajax-ify the listbox so that no full postback occurs. Quote
Armaggoth Posted February 23, 2010 Posted February 23, 2010 (edited) This implementation does it all on a single for loop, it's controlled by the index parameter, it must be -1 to move the item up (the index decreases) or 1 to move the item down (the index increases). It also works on a multiple selection listbox. The loop dynamically adjusts its direction depending on the index parameter: private void MoveListboxItem(int index, ListBox listBox) { if (listBox.SelectedIndex != -1) //is there an item selected? { //if it's moving up, the loop moves from first to last, otherwise, it moves from last to first for (int i = (index < 0 ? 0 : listBox.Items.Count - 1); index < 0 ? i < listBox.Items.Count : i > -1; i -= index) { if (listBox.Items[i].Selected) { //if it's moving up, it should not be the first item, or, if it's moving down, it should not be the last if ((index < 0 && i > 0) || (index > 0 && i < listBox.Items.Count - 1)) { //if it's moving up, the previous item should not be selected, or, if it's moving down, the following item should not be selected if ((index < 0 && !listBox.Items[i - 1].Selected) || (index > 0 && !listBox.Items[i + 1].Selected)) { ListItem itemA = listBox.Items[i]; //the selected item listBox.Items.Remove(itemA); //is removed listBox.Items.Insert(i + index, itemA);//and swapped } } } } } } Edited February 24, 2010 by snarfblam Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.