The items in the two listboxes move each other and move them up and down.

Source: Internet
Author: User

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">

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.