gpt4 book ai didi

c# - 如何允许用户更改列表框顺序

转载 作者:行者123 更新时间:2023-11-30 21:02:03 24 4
gpt4 key购买 nike

我正在寻求帮助,我有两个列表,它们都将数据添加到同一个列表框并将它们显示为摘要,我想知道如何让用户在列表框中向上或向下移动索引.

项目在这里添加

   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
}

我的其他类(class)也是如此,所以任何帮助将不胜感激

最佳答案

你可以尝试这样的事情。下面的代码假定一个 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;
}
}
}

这仅在您使用 ObjectCollection.Add 方法将项目添加到 ListBox 时才有效。如果是数据绑定(bind),可以更新实际的数据源,使用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();
}

关于c# - 如何允许用户更改列表框顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13714837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com