- #region 对LISTBOX控件操作
- /// <summary>
- /// 新增数据
- /// </summary>
- /// <param name="Src"></param>
- /// <param name="E"></param>
- protectedvoid 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>
- protectedvoid 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>
- protectedvoid 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>
- protectedvoid 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
#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:已选项目列表
说明:可以实现一次性将所有列表框项目移到另一个列表框中。可以将列表任意选中项目移动到另一个列表框中。
经本人测试通过,方法可以实现