如何允许用户更改列表框顺序 [英] How to allow user to change list box order

查看:101
本文介绍了如何允许用户更改列表框顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am looking for help, I have two lists that both add data to the same list box and it displays them as a summary, I would like to know how to let the user move an index up or down in the list box.

在此处添加项目

   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
    }

这与我其他班级的情况相同,因此,我们将不胜感激

This is the same for my other class, So any help would be appreciated

推荐答案

您可以尝试类似的方法.以下代码假定Windows窗体包含一个名为mainListBox的列表框,一个名为upButton的按钮和一个名为downButton的按钮.

You can try something like this. The following code assumes a Windows form containing a ListBox named mainListBox, a button named upButton, and a button named 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时有效.如果您要进行数据绑定,则可以更新实际的数据源,并使用ListBox的BindingContext刷新.

This will only work if you are adding items to the ListBox using the ObjectCollection.Add method. If you are data binding, you can update the actual data source and use the ListBox's BindingContext to refresh.

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();
}

这篇关于如何允许用户更改列表框顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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