昊仔

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1. #region  对LISTBOX控件操作  
  2.  
  3.        /// <summary>  
  4.        /// 新增数据  
  5.        /// </summary>  
  6.        /// <param name="Src"></param>  
  7.        /// <param name="E"></param>  
  8.        protectedvoid AddBtn_Click(Object Src, EventArgs E) 
  9.        { 
  10.            if (lbPrograms.SelectedIndex != -1) 
  11.            { 
  12.                int iItemCount = lbPrograms.Items.Count; 
  13.                string sItemStr = string.Empty; 
  14.                for (int i = 0; i < iItemCount; i++) 
  15.                { 
  16.                    if (lbPrograms.Items[i].Selected == true
  17.                    { 
  18.                        lbSelectedPrograms.Items.Add(new ListItem(lbPrograms.Items[i].Text, lbPrograms.Items[i].Value));                       
  19.                        sItemStr += lbPrograms.Items[i].Value + "|"
  20.                    } 
  21.                } 
  22.                sItemStr = sItemStr.Substring(0, sItemStr.Length - 1); 
  23.  
  24.                string[] sItemList = sItemStr.Split('|'); 
  25.                 
  26.                for (int i = 0; i < sItemList.Length; i++) 
  27.                { 
  28.                    lbPrograms.Items.Remove(sItemList[i]); 
  29.                } 
  30.                 
  31.            } 
  32.        } 
  33.        /// <summary>  
  34.        /// 新增所有数据  
  35.        /// </summary>  
  36.        /// <param name="Src"></param>  
  37.        /// <param name="E"></param>  
  38.        protectedvoid AddAllBtn_Click(Object Src, EventArgs E) 
  39.        { 
  40.            while (lbPrograms.Items.Count != 0) 
  41.            { 
  42.                lbSelectedPrograms.Items.Add(new ListItem(lbPrograms.Items[0].Text, lbPrograms.Items[0].Value)); 
  43.                lbPrograms.Items.Remove(lbPrograms.Items[0]); 
  44.            } 
  45.        } 
  46.        /// <summary>  
  47.        /// 删除数据  
  48.        /// </summary>  
  49.        /// <param name="Src"></param>  
  50.        /// <param name="E"></param>  
  51.        protectedvoid RemoveBtn_Click(Object Src, EventArgs E) 
  52.        { 
  53.            if (lbSelectedPrograms.SelectedIndex != -1) 
  54.            { 
  55.                int iItemCount = lbSelectedPrograms.Items.Count; 
  56.                string sItemStr = string.Empty; 
  57.                for (int i = 0; i < iItemCount; i++) 
  58.                { 
  59.                    if (lbSelectedPrograms.Items[i].Selected == true
  60.                    { 
  61.                        lbPrograms.Items.Add(new ListItem(lbSelectedPrograms.Items[i].Text, lbSelectedPrograms.Items[i].Value)); 
  62.                        sItemStr += lbSelectedPrograms.Items[i].Value + "|"
  63.                    } 
  64.                } 
  65.                sItemStr = sItemStr.Substring(0, sItemStr.Length - 1); 
  66.  
  67.                string[] sItemList = sItemStr.Split('|'); 
  68.  
  69.                for (int i = 0; i < sItemList.Length; i++) 
  70.                { 
  71.                    lbSelectedPrograms.Items.Remove(sItemList[i]); 
  72.                } 
  73.  
  74.            } 
  75.        } 
  76.        /// <summary>  
  77.        /// 删除所有数据  
  78.        /// </summary>  
  79.        /// <param name="Src"></param>  
  80.        /// <param name="E"></param>  
  81.        protectedvoid RemoveAllBtn_Click(Object Src, EventArgs E) 
  82.        { 
  83.            while (lbSelectedPrograms.Items.Count != 0) 
  84.            {                 
  85.                lbPrograms.Items.Add(new ListItem(lbSelectedPrograms.Items[0].Text, lbSelectedPrograms.Items[0].Value)); 
  86.                lbSelectedPrograms.Items.Remove(lbSelectedPrograms.Items[0]); 
  87.            } 
  88.        }
  89.        #endregion 
 #region  对LISTBOX控件操作

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="Src"></param>
        /// <param name="E"></param>
        protected void AddBtn_Click(Object Src, EventArgs E)
        {
            if (lbPrograms.SelectedIndex != -1)
            {
                int iItemCount = lbPrograms.Items.Count;
                string sItemStr = string.Empty;
                for (int i = 0; i < iItemCount; i++)
                {
                    if (lbPrograms.Items[i].Selected == true)
                    {
                        lbSelectedPrograms.Items.Add(new ListItem(lbPrograms.Items[i].Text, lbPrograms.Items[i].Value));                      
                        sItemStr += lbPrograms.Items[i].Value + "|";
                    }
                }
                sItemStr = sItemStr.Substring(0, sItemStr.Length - 1);

                string[] sItemList = sItemStr.Split('|');
                
                for (int i = 0; i < sItemList.Length; i++)
                {
                    lbPrograms.Items.Remove(sItemList[i]);
                }
                
            }
        }
        /// <summary>
        /// 新增所有数据
        /// </summary>
        /// <param name="Src"></param>
        /// <param name="E"></param>
        protected void AddAllBtn_Click(Object Src, EventArgs E)
        {
            while (lbPrograms.Items.Count != 0)
            {
                lbSelectedPrograms.Items.Add(new ListItem(lbPrograms.Items[0].Text, lbPrograms.Items[0].Value));
                lbPrograms.Items.Remove(lbPrograms.Items[0]);
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="Src"></param>
        /// <param name="E"></param>
        protected void RemoveBtn_Click(Object Src, EventArgs E)
        {
            if (lbSelectedPrograms.SelectedIndex != -1)
            {
                int iItemCount = lbSelectedPrograms.Items.Count;
                string sItemStr = string.Empty;
                for (int i = 0; i < iItemCount; i++)
                {
                    if (lbSelectedPrograms.Items[i].Selected == true)
                    {
                        lbPrograms.Items.Add(new ListItem(lbSelectedPrograms.Items[i].Text, lbSelectedPrograms.Items[i].Value));
                        sItemStr += lbSelectedPrograms.Items[i].Value + "|";
                    }
                }
                sItemStr = sItemStr.Substring(0, sItemStr.Length - 1);

                string[] sItemList = sItemStr.Split('|');

                for (int i = 0; i < sItemList.Length; i++)
                {
                    lbSelectedPrograms.Items.Remove(sItemList[i]);
                }

            }
        }
        /// <summary>
        /// 删除所有数据
        /// </summary>
        /// <param name="Src"></param>
        /// <param name="E"></param>
        protected void RemoveAllBtn_Click(Object Src, EventArgs E)
        {
            while (lbSelectedPrograms.Items.Count != 0)
            {                
                lbPrograms.Items.Add(new ListItem(lbSelectedPrograms.Items[0].Text, lbSelectedPrograms.Items[0].Value));
                lbSelectedPrograms.Items.Remove(lbSelectedPrograms.Items[0]);
            }
        }
        #endregion

 

 

lbPrograms:待选项目列表

lbSelectedPrograms:已选项目列表

 

说明:可以实现一次性将所有列表框项目移到另一个列表框中。可以将列表任意选中项目移动到另一个列表框中。

经本人测试通过,方法可以实现

posted on 2013-08-23 17:56  昊仔  阅读(541)  评论(0编辑  收藏  举报