Control (1) -- Implement shift of specified entries in the list box and move the pointer to the specified position

Source: Internet
Author: User

The theme of the day is about C # basics, vs controls, and ado.net. Among the three, the controls in vs are described in great detail.
The following describes how to use The ListBox control.

The controls used in this example include labels, ListBox, and buttons.

As follows:

 

Set the selectionmode attribute of the list box to multiple to select multiple items.

The Code is as follows:

 

Protected void page_load (Object sender, eventargs e) {label2.text = "sort the following heroes to your liking:"; label2.font. bold = true; label2.font. size = 16;} protected void btnup_click (Object sender, eventargs e) {// move an IF (button) sender) up or down ). commandname = "up" & lsbshow. selectedindex> 0 | (button) sender ). commandname = "down" & lsbshow. selectedindex <lsbshow. items. count-1) {// determine whether the sent command must be up and the index of the selected entry must be greater than 0, or down, and the selected entry must rain down the int index; // to reduce the code, here we will make a judgment on the variable, and then directly call the variable if (button) sender ). commandname = "up") {Index =-1; // The subsequent index will be added to or subtracted from the current index} else {Index = 1 ;} listitem lT = new listitem (lsbshow. selecteditem. text, lsbshow. selectedvalue); lsbshow. items [lsbshow. selectedindex]. TEXT = lsbshow. items [lsbshow. selectedindex + Index]. text; lsbshow. items [lsbshow. selectedindex]. value = lsbshow. items [lsbshow. selectedindex + Index]. value; lsbshow. items [lsbshow. selectedindex + Index]. TEXT = lt. text; lsbshow. items [lsbshow. selectedindex + Index]. value = lt. value; lsbshow. selectedindex = lsbshow. selectedindex + index;} protected void btnfirst_click (Object sender, eventargs e) {// to the first lsbshow. selectedindex = 0;} protected void btnlast_click (Object sender, eventargs e) {// to the last lsbshow. selectedindex = lsbshow. items. count-1;} protected void btnbefore_click (Object sender, eventargs e) {// The Last lsbshow. selectedindex = lsbshow. selectedindex-1;} protected void btnnext_click (Object sender, eventargs e) {// next lsbshow. selectedindex = lsbshow. selectedindex + 1 ;}

 

The "btnup_click" event is returned when the move up and move down button is clicked. The corresponding code is as follows:

<P> <asp: button id = "btnup" runat = "server" text = "Move Up" commandname = "up" onclick = "btnup_click"/> <asp: button id = "btndown" runat = "server" text = "Move Down" commandname = "down" onclick = "btnup_click"/> </P>

 

The effect is as follows:

 

 

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.