C# 如何允许用户更改列表框顺序
我正在寻求帮助,我有两个列表,都将数据添加到同一个列表框中,并将它们显示为摘要,我想知道如何让用户在列表框中上下移动索引 在此处添加项目C# 如何允许用户更改列表框顺序,c#,list,listbox,C#,List,Listbox,我正在寻求帮助,我有两个列表,都将数据添加到同一个列表框中,并将它们显示为摘要,我想知道如何让用户在列表框中上下移动索引 在此处添加项目 private void BtnAddpickup_Click(object sender, EventArgs e) { /* * This method creates a new pickup object, allows the user to * enter deta
private void BtnAddpickup_Click(object sender, EventArgs e)
{
/*
* This method creates a new pickup object, allows the user to
* enter details and adds it to the List
*
*/
Pickupform.pickup = new Pickups();
//New Visit- note added to the pickupform object
Pickupform.ShowDialog();
//Show the pickupForm. ShowDialog ensures that the form has the exclusive focus until it is closed.
if (Pickupform.pickup != null)
//if null then the "cancel" button was pressed
{
Pickups newpic = Pickupform.pickup;
//Get the Pickup object from the form
thePickup.addPickups(newpic);
//Add the visit to the list
}
updateList();
//Update the list object to reflect the Pickups in the list
}
还有这个
public Pickups getPickups(int index)
{
//Return the pickup object at the <index> place in the list
int count = 0;
foreach (Pickups pic in pickups)
{
//Go through all the pickup objects
if (index == count)
//If we're at the correct point in the list...
return pic;
//exit this method and return the current visit
count++;
//Keep counting
}
return null;
//Return null if an index was entered that could not be found
}
public Pickups getPickups(int索引)
{
//在列表中的位置返回拾取对象
整数计数=0;
foreach(皮卡中的皮卡图片)
{
//检查所有拾取对象
如果(索引==计数)
//如果我们在列表中的正确位置。。。
返回pic;
//退出此方法并返回当前访问
计数++;
//数一数
}
返回null;
//如果输入了找不到的索引,则返回null
}
这对我的另一门课也是一样的,因此如果您能提供任何帮助,我们将不胜感激。您可以尝试类似的方法。下面的代码假定Windows窗体包含名为mainListBox的列表框、名为upButton的按钮和名为downButton的按钮
public partial class Form1 : Form
{
private class Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public override string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}
public Form1()
{
this.InitializeComponent();
this.mainListBox.SelectionMode = SelectionMode.One;
this.PopulateListBox();
}
private void PopulateListBox()
{
this.mainListBox.Items.Add(new Person() { FirstName = "Joe", LastName = "Smith" });
this.mainListBox.Items.Add(new Person() { FirstName = "Sally", LastName = "Jones" });
this.mainListBox.Items.Add(new Person() { FirstName = "Billy", LastName = "Anderson" });
}
private void upButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > 0)
{
int selectedIndex = this.mainListBox.SelectedIndex;
object selectedItem = this.mainListBox.SelectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex - 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex - 1;
}
}
private void downButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > -1 &&
this.mainListBox.SelectedIndex < this.mainListBox.Items.Count - 1)
{
int selectedIndex = this.mainListBox.SelectedIndex;
object selectedItem = this.mainListBox.SelectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex + 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex + 1;
}
}
}
公共部分类表单1:表单
{
私人阶级人士
{
公共字符串LastName{get;set;}
公共字符串名{get;set;}
公共重写字符串ToString()
{
返回string.Format(“{0},{1}”,LastName,FirstName);
}
}
公共表格1()
{
this.InitializeComponent();
this.mainListBox.SelectionMode=SelectionMode.One;
this.PopulateListBox();
}
私有void PopulateListBox()
{
this.mainListBox.Items.Add(newperson(){FirstName=“Joe”,LastName=“Smith”});
这个.mainListBox.Items.Add(newperson(){FirstName=“Sally”,LastName=“Jones”});
这个.mainListBox.Items.Add(newperson(){FirstName=“Billy”,LastName=“Anderson”});
}
私有无效向上按钮单击(对象发送方,事件参数e)
{
如果(this.mainListBox.SelectedIndex>0)
{
int selectedIndex=this.mainListBox.selectedIndex;
object selectedItem=this.mainListBox.selectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex-1,selectedItem);
this.mainListBox.SelectedIndex=SelectedIndex-1;
}
}
private void downButton\u单击(对象发送者,事件参数e)
{
如果(this.mainListBox.SelectedIndex>-1&&
this.mainListBox.SelectedIndex
仅当使用ObjectCollection.Add方法将项添加到列表框时,此操作才有效。如果是数据绑定,则可以更新实际数据源并使用ListBox的BindingContext进行刷新
private List<Person> people = new List<Person>();
private void PopulateListBox()
{
this.people.Add(new Person() { FirstName = "Joe", LastName = "Smith" });
this.people.Add(new Person() { FirstName = "Sally", LastName = "Jones" });
this.people.Add(new Person() { FirstName = "Billy", LastName = "Anderson" });
this.mainListBox.DataSource = people;
}
private void upButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > 0)
{
int selectedIndex = this.mainListBox.SelectedIndex;
Person selectedItem = this.mainListBox.SelectedItem as Person;
this.people.RemoveAt(selectedIndex);
this.people.Insert(selectedIndex - 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex - 1;
this.RefreshListSource();
}
}
private void RefreshListSource()
{
CurrencyManager boundList = this.mainListBox.BindingContext[this.people] as CurrencyManager;
boundList.Refresh();
}
private List people=new List();
私有void PopulateListBox()
{
this.people.Add(newperson(){FirstName=“Joe”,LastName=“Smith”});
this.people.Add(newperson(){FirstName=“Sally”,LastName=“Jones”});
this.people.Add(newperson(){FirstName=“Billy”,LastName=“Anderson”});
this.mainListBox.DataSource=人;
}
私有无效向上按钮单击(对象发送方,事件参数e)
{
如果(this.mainListBox.SelectedIndex>0)
{
int selectedIndex=this.mainListBox.selectedIndex;
Person-selectedItem=this.mainListBox.selectedItem为Person;
this.people.RemoveAt(selectedIndex);
this.people.Insert(selectedIndex-1,selectedItem);
this.mainListBox.SelectedIndex=SelectedIndex-1;
此参数为.RefreshListSource();
}
}
私有void RefreshListSource()
{
CurrencyManager boundList=this.mainListBox.BindingContext[this.people]作为CurrencyManager;
刷新();
}
您可以尝试这样的方法。下面的代码假定Windows窗体包含名为mainListBox的列表框、名为upButton的按钮和名为downButton的按钮
public partial class Form1 : Form
{
private class Person
{
public string LastName { get; set; }
public string FirstName { get; set; }
public override string ToString()
{
return string.Format("{0}, {1}", LastName, FirstName);
}
}
public Form1()
{
this.InitializeComponent();
this.mainListBox.SelectionMode = SelectionMode.One;
this.PopulateListBox();
}
private void PopulateListBox()
{
this.mainListBox.Items.Add(new Person() { FirstName = "Joe", LastName = "Smith" });
this.mainListBox.Items.Add(new Person() { FirstName = "Sally", LastName = "Jones" });
this.mainListBox.Items.Add(new Person() { FirstName = "Billy", LastName = "Anderson" });
}
private void upButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > 0)
{
int selectedIndex = this.mainListBox.SelectedIndex;
object selectedItem = this.mainListBox.SelectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex - 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex - 1;
}
}
private void downButton_Click(object sender, EventArgs e)
{
if (this.mainListBox.SelectedIndex > -1 &&
this.mainListBox.SelectedIndex < this.mainListBox.Items.Count - 1)
{
int selectedIndex = this.mainListBox.SelectedIndex;
object selectedItem = this.mainListBox.SelectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex + 1, selectedItem);
this.mainListBox.SelectedIndex = selectedIndex + 1;
}
}
}
公共部分类表单1:表单
{
私人阶级人士
{
公共字符串LastName{get;set;}
公共字符串名{get;set;}
公共重写字符串ToString()
{
返回string.Format(“{0},{1}”,LastName,FirstName);
}
}
公共表格1()
{
this.InitializeComponent();
this.mainListBox.SelectionMode=SelectionMode.One;
this.PopulateListBox();
}
私有void PopulateListBox()
{
this.mainListBox.Items.Add(newperson(){FirstName=“Joe”,LastName=“Smith”});
这个.mainListBox.Items.Add(newperson(){FirstName=“Sally”,LastName=“Jones”});
这个.mainListBox.Items.Add(newperson(){FirstName=“Billy”,LastName=“Anderson”});
}
私有无效向上按钮单击(对象发送方,事件参数e)
{
如果(this.mainListBox.SelectedIndex>0)
{
int selectedIndex=this.mainListBox.selectedIndex;
object selectedItem=this.mainListBox.selectedItem;
this.mainListBox.Items.RemoveAt(selectedIndex);
this.mainListBox.Items.Insert(selectedIndex-1,selectedItem);
this.mainListBox.SelectedIndex=SelectedIndex-1;
}
}
private void downButton\u单击(对象发送者,事件参数e)
{
如果(this.mainListBox.SelectedIndex>-1&&
this.mainListBox.SelectedIndex