如何将项目从列表框移动到另一个列表框 [英] How to move item(s) from a listbox to another

查看:186
本文介绍了如何将项目从列表框移动到另一个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含两个列表框(源和目标)的WPF应用程序

和两个按钮(>>按钮< < button )从另一个列表框中移动(不复制)一个或多个项目。



来源当窗口加载时,列表框用预定义的值初始化(例如1,2,3,4)。



另一件事,当一个或多个项目被移回时从目标列表框到列表框,它按原始顺序插入(如窗口加载时)。



我的解决方案的问题是,当我点击(<<<按钮)时,每次点击时,MA7 arraylist项目中最后保存的项目都会被复制(<< button)



谢谢。

I want to make a WPF application that has two list boxes (source and target)
and two buttons(>> button and << button) to move (not copy) one or multiple items from a list box the other.

The source list box is initialized with predefined values when the window loads (1,2,3,4 for example).

Another thing, when one or multiple items are moved back from the target list box to the source list box, it is inserted on its original order (as when the window has loaded).

The problem with my solution is that when I click the (<< button) the last saved items in MA7 arraylist items gets copied every time i click the (<< button)

Thank you.

推荐答案

如果你是寻找下面的东西



If you are looking for something like below

Listbox1    ListBox2
   1               a
   2               b
   3               c



现在你有按钮一>>和按钮2<



按钮1(>>)点击项目应该进入Listbox2

按钮2( <<)点击项目应该进入Listbox1

如果移动后在列表框中遇到问题,则需要向Listbox显示数据源。

如下所示

Items.Add(something);

Listbox1.Datasource = Items;



告诉我你是否遇到问题


Now you have button one >> and button two <<

on button one(>>) click item should go in Listbox2
On button two(<<) click item should go in Listbox1
If you are having trouble in listbox after moving, you need to show datasource to Listbox.
something like below
Items.Add("something");
Listbox1.Datasource=Items;

let me know if you are having problems


这就是你要找的东西。 ASP.Snippets对于代码片段非常棒

http://www.aspsnippets.com/Articles/How-to-move-ListBox-items-to-another-ListBox-in-ASPNet-using-jQuery.aspx [ ^ ]



如果这有助于您接受它作为解决方案
This is what you are looking for. ASP.Snippets is awesome for code snippets
http://www.aspsnippets.com/Articles/How-to-move-ListBox-items-to-another-ListBox-in-ASPNet-using-jQuery.aspx[^]

If this has helped you accept it as solution


使用系统;

使用System.Collections;

使用System.Collections.Generic;

使用System.ComponentModel;

使用System.Data;

使用System.Drawing;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用System.Windows.Forms;



命名空间From_To_Listboxes

{

公共部门类Form1:表格

{

ArrayList MyArray = new ArrayList();

ArrayList SortedArray = new ArrayList();

ArrayList MA1 = new ArrayList ();

ArrayList MA2 = new ArrayList();

ArrayList MA3 = new ArrayList();

ArrayList MA4 = new ArrayList() ;

ArrayList MA5 = new ArrayList();

ArrayList MA6 = new ArrayList();

ArrayList MA7 = new ArrayList();

ArrayList SelChangedFrom = new ArrayList();

ArrayList SelChangedTo = new ArrayList();

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender,EventArgs e)

{

lstbxFrom.SelectionMode = SelectionMode.MultiExtended;

lstbxTo.SelectionMode = SelectionMode.MultiExtended;



MyArray.Add(1);

MyArray.Add(2);

MyArray.Add(3);

MyArray.Add(4);

MyArray.Add(5);

MyArray.Add(6);

MyArray.Add(7);

foreach(MyArray中的字符串项目)

{

lstbxFrom。 Items.Add(item);

}

}

private void btnFromTo_Click(object sender,EventArgs e)

{



lstbxTo.ClearSelected();



foreach(lstbxFrom.Items中的字符串项)

{

MA1.Add(item);

}

foreach(lstbxFrom.SelectedItems中的字符串项)

{

MA2.Add(item);





}

foreach(MA2中的字符串项目)

{

lstbxTo.Items.Add(item);

}

foreach(MA1中的字符串项)

{

if(!(MA1.Contains(item)&& MA2.Contains(item)))

{

MA3.Add(item);

}



}

lstbxFrom.Items.Clear();





foreach(MA3中的字符串项)

{

lstbxFrom.Items.Add(item);

}



MA1.Clear();

MA2.Clear();

MA3.Clear();

}

private void btnToFrom_Click(object sender,EventArgs e)

{

lstbxFrom.ClearSelected();

if(lstbxTo.S electionsItems.Count!= 0)

{

foreach(lstbxTo.Items中的字符串项)

{

MA4.Add(item);

}

foreach(lstbxTo.SelectedItems中的字符串项目)

{

MA5.Add(item);

}



foreach(MA5中的字符串项)

{

MA7.Add(item);

}

foreach(MA1中的字符串项)

{

MA7.Add(item);

}





foreach(MA7中的字符串项) )

{

lstbxFrom.Items.Add(item);

}





foreach(字符串项目in MA4)

{

if(!(MA4.Contains(item)&& MA5.Contains(item)))

{

MA6.Add(item);

}

}



lstbxTo.Items。清除();

foreach(MA6中的字符串项)

{

lstbxTo.Items.Add(item);

}

}

foreach(lstbxFrom.Items中的字符串项)

{

SortedArray.Add (item);

}

SortedArray.Sort();

lstbxFrom.Items.Clear();

foreach(SortedArray中的字符串项目)

{

lstbxFrom.Items.Add(item);

}

SortedArray.Clear();

MA4.Clear();

MA5.Clear();

MA6.Clear();

MA7.Clear();

}

}

}
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace From_To_Listboxes
{
public partial class Form1 : Form
{
ArrayList MyArray = new ArrayList();
ArrayList SortedArray = new ArrayList();
ArrayList MA1 = new ArrayList();
ArrayList MA2 = new ArrayList();
ArrayList MA3 = new ArrayList();
ArrayList MA4 = new ArrayList();
ArrayList MA5 = new ArrayList();
ArrayList MA6 = new ArrayList();
ArrayList MA7 = new ArrayList();
ArrayList SelChangedFrom = new ArrayList();
ArrayList SelChangedTo = new ArrayList();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
lstbxFrom.SelectionMode = SelectionMode.MultiExtended;
lstbxTo.SelectionMode = SelectionMode.MultiExtended;

MyArray.Add("1");
MyArray.Add("2");
MyArray.Add("3");
MyArray.Add("4");
MyArray.Add("5");
MyArray.Add("6");
MyArray.Add("7");
foreach (string item in MyArray)
{
lstbxFrom.Items.Add(item);
}
}
private void btnFromTo_Click(object sender, EventArgs e)
{

lstbxTo.ClearSelected();

foreach (string item in lstbxFrom.Items)
{
MA1.Add(item);
}
foreach (string item in lstbxFrom.SelectedItems)
{
MA2.Add(item);


}
foreach (string item in MA2)
{
lstbxTo.Items.Add(item);
}
foreach (string item in MA1)
{
if (!(MA1.Contains(item) && MA2.Contains(item)))
{
MA3.Add(item);
}

}
lstbxFrom.Items.Clear();


foreach (string item in MA3)
{
lstbxFrom.Items.Add(item);
}

MA1.Clear();
MA2.Clear();
MA3.Clear();
}
private void btnToFrom_Click(object sender, EventArgs e)
{
lstbxFrom.ClearSelected();
if (lstbxTo.SelectedItems.Count != 0)
{
foreach (string item in lstbxTo.Items)
{
MA4.Add(item);
}
foreach (string item in lstbxTo.SelectedItems)
{
MA5.Add(item);
}

foreach (string item in MA5)
{
MA7.Add(item);
}
foreach (string item in MA1)
{
MA7.Add(item);
}


foreach (string item in MA7)
{
lstbxFrom.Items.Add(item);
}


foreach (string item in MA4)
{
if (!(MA4.Contains(item) && MA5.Contains(item)))
{
MA6.Add(item);
}
}

lstbxTo.Items.Clear();
foreach (string item in MA6)
{
lstbxTo.Items.Add(item);
}
}
foreach (string item in lstbxFrom.Items)
{
SortedArray.Add(item);
}
SortedArray.Sort();
lstbxFrom.Items.Clear();
foreach (string item in SortedArray)
{
lstbxFrom.Items.Add(item);
}
SortedArray.Clear();
MA4.Clear();
MA5.Clear();
MA6.Clear();
MA7.Clear();
}
}
}


这篇关于如何将项目从列表框移动到另一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆