Reference: http://www.cnblogs.com/greatverve/archive/2012/03/27/listbox-add-remove-up-down.html
It seems that codeproject has a very powerful similar control, and there is no need to use a custom control here.
Moving left and right is a simple option to add and delete items. Advanced syntax is used for moving up and down, which is worth learning.
UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;
Using Winform = system. Windows. forms; Public Static Class Listboxextension
{
Public Static Bool Moveselecteditems ( This Winform. ListBox, Bool ISUP, Action noselectaction)
{
If (ListBox. selecteditems. Count> 0 )
{
Return ListBox. moveselecteditems (ISUP );
}
Else
{
Noselectaction ();
Return False ;
}
}
Public Static Bool Moveselecteditems ( This Winform. ListBox, Bool ISUP)
{
Bool Result = True ;
Winform. ListBox. selectedindexcollection indices = ListBox. selectedindices;
If (ISUP)
{
If (ListBox. selecteditems. Count> 0 & Indices [ 0 ]! = 0 )
{
Foreach ( Int I In Indices)
{
Result & = moveselecteditem (ListBox, I, True );
}
}
}
Else
{
If (ListBox. selecteditems. Count> 0 & Indices [indices. Count- 1 ]! = ListBox. Items. Count- 1 )
{
For ( Int I = indices. Count- 1 ; I> = 0 ; I --)
{
Result & = moveselecteditem (ListBox, indices [I], False );
}
}
}
Return Result;
}
Public Static Bool Moveselecteditem ( This Winform. ListBox, Bool ISUP, Action noselectaction)
{
If (ListBox. selecteditems. Count> 0 )
{
Return Moveselecteditem (ListBox, ListBox. selectedindex, ISUP );
}
Else
{
Noselectaction ();
Return False ;
}
}
Public Static BoolMoveselecteditem (ThisWinform. ListBox,BoolISUP)
{
ReturnMoveselecteditem (ListBox, ListBox. selectedindex, ISUP );
}
Private Static Bool Moveselecteditem ( This Winform. ListBox, Int Selectedindex,Bool ISUP)
{
If (Selectedindex! = (ISUP? 0 : ListBox. Items. Count- 1 ))
{
Object Current = ListBox. items [selectedindex];
Int Insertat = selectedindex + (ISUP? -1 : 1 );
ListBox. Items. removeat (selectedindex );
ListBox. Items. insert (insertat, current );
ListBox. selectedindex = insertat;
Return True;
}
Return False;
}
}
This class is probably looked at and written very well. No, just use it.Public Partial ClassFrmreportset: Form
{
PublicFrmreportset ()
{
Initializecomponent ();
}
Private Void Btnadd_click ( Object Sender, eventargs E)
{
List <Object> listobj = New List <Object > ();
Foreach (Object OBJ In Lboxcanuse. selecteditems)
{
Lboxselected. Items. Add (OBJ );
Listobj. Add (OBJ );
}
Foreach (Object OBJ In Listobj)
{
Lboxcanuse. Items. Remove (OBJ );
}
}
Private Void Btnremove_click ( Object Sender, eventargs E)
{
List <Object> listobj = New List < Object > ();
Foreach (Object OBJ In Lboxselected. selecteditems)
{
Lboxcanuse. Items. Add (OBJ );
Listobj. Add (OBJ );
}
Foreach (Object OBJ In Listobj)
{
Lboxselected. Items. Remove (OBJ );
}
}
private void btnup_click ( Object sender, eventargs E)
{< br> This . lboxselected. moveselecteditems ( true , () =>
{< br> MessageBox. show ( " select " );
});
}
private void btndown_click ( Object sender, eventargs E)
{< br> This . lboxselected. moveselecteditems ( false , () =>
{< br> MessageBox. show ( " select " );
});
}< BR >}< SPAN class = "cnblogs_code_copy">