---
title: "ListBox应用"
author: "njmnjx"
date: "2010-11-13T12:00:52Z"
tags: []
categories: ["编程语言"]
slug: "listbox"
draft: false
lastmod: "2010-11-13T12:05:51Z"
id: 810496
url: "https://njm.iteye.com/blog/810496"
---

```c#

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Collections;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    //全选
    protected void selAll_Click(object sender, EventArgs e)
    {
        ListBox2.Items.Clear();
        foreach(ListItem li in ListBox1.Items)
        {
            if (!EmHas(ListBox2, li))
            {
                ListBox2.Items.Add(li);
            }
        }
    }

    //单选
    protected void selSingle_Click(object sender, EventArgs e)
    {
        ListItem li = ListBox1.SelectedItem;
        if (li == null)
        {
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "javascript:alert('请选择内容');", true);
            return;
        }

        if (!EmHas(ListBox2, li))
        {
            ListBox2.Items.Add(li);
        }

        ListBoxSort(ListBox2);

    }

    //全不选
    protected void delAll_Click(object sender, EventArgs e)
    {
        ListBox2.Items.Clear();
    }

    //剔除单选内容
    protected void delSingle_Click(object sender, EventArgs e)
    {
        
        ListItem li = ListBox2.SelectedItem;
        if (li == null)
        {
            ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "javascript:alert('请选择内容');", true);
            return;
        }
        ListBox2.Items.Remove(li);
        
    }

    //判断重复
    protected bool EmHas(ListBox lb,ListItem li)
    {
        return lb.Items.Contains(li);
    }

    //确认查看
    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        foreach(ListItem li in ListBox2.Items)
        {
            sb.Append("文本："+li.Text + "  值：" + li.Value + "\\n");
        }

        ClientScript.RegisterClientScriptBlock(Page.GetType(), "", "javascript:alert('选中内容展示：\\n"+sb.ToString()+"');", true);
    }

    //利用SortedList排序
    private void ListBoxSort(ListBox lbx)  //利用sortedlist 类为listbox排序
    {
        SortedList slist = new SortedList();
        for (int i = 0; i < lbx.Items.Count; i++)
        {
            slist.Add(lbx.Items[i].Value, lbx.Items[i].Text);  //将listbox内容逐项复制到sortedlist对象中
        }


        lbx.Items.Clear();//清空原listbox


        foreach (DictionaryEntry obj in slist)
        {
            ListItem myit = new ListItem();
            myit.Text = obj.Value.ToString();
            myit.Value = obj.Key.ToString();
            lbx.Items.Add(myit);               //再重新将sortlist集合复制回listbox，这样，复制回来的数组是按值排序过的
           
        }
    }
    
}


```


<span style="color:green">效果图</span>

![](http://dl.iteye.com/upload/attachment/347633/0493ec32-0aea-39cf-9d78-75abf16eb7a9.png)


<span style="color:green">原码</span>
<http://dl.iteye.com/topics/download/4703eef5-0360-3ef5-a94b-f29967cebc83>