使用ShowDialog表单中的checkedlistbox对列表框进行排序 [英] Sorting a listbox using checkedlistbox in ShowDialog form

查看:107
本文介绍了使用ShowDialog表单中的checkedlistbox对列表框进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从列表uList填充列表框listBoxHome:



I fill a listbox "listBoxHome" from a list "uList" :

uList.Add(new KeyValuePair<int, string>(item.Id, item.Name));
     listBoxHome.DataSource = uList;
     listBoxHome.DisplayMember = "Value";
     listBoxHome.ValueMember = "Key";



我还有一个按钮CHANGE。当我按下CHANGE按钮时,我使用Showdialog打开一个名为Sub的新表单


I also have a button "CHANGE". When I push button CHANGE I use Showdialog to open a new form called Sub

Sub sub = new Sub();
 sub.ShowDialog();





子表单有一个按钮MakeChange和一个checkedlistbox。

我必须从listboxHome的项目填写这个checkedlistbox。使用checkedlistbox,当我检查5个项目并按Makechange时我想将这些项目放在listBoxHome的前5个位置。未经检查的项目必须填写其他地方listBoxHome.Also,前5个地方必须排序asc,其他(工作台)地方也排序asc。任何想法?



The Sub form has one button "MakeChange" and a checkedlistbox.
I must fill this checkedlistbox from the items of listboxHome.Using the checkedlistbox, when I check 5 items and press "Makechange" I want to place this items in first 5 places in listBoxHome.The unchecked items must fill the other places of listBoxHome.Also, The first 5 places must sorted asc and the other (bench)places also sorted asc. Any ideas?

推荐答案

在使用CheckedListBox一段时间之后,我确信您会发现使用两个ListBox实现它更容易。 ListBox通过将其Sorted Property设置为true来为您提供内置排序。



如果您仍然真的希望使用CheckedListBox,希望此代码能够为您提供使用的基础。



在表单上放置两个ListBox:设置'listBox2以保存所有可能选择的集合;将'listBox1留空。将两个ListBox的排序属性设置为'true。
After playing with the CheckedListBox a while, I am convinced you will find it much easier to implement this using two ListBoxes. The ListBox offers you built-in sorting by setting its 'Sorted Property to 'true.

If you still really wish to use a CheckedListBox, hopefully this code will give you a basis to work with.

Put two ListBoxes on a Form: set 'listBox2 to hold the set of all your possible selections; leave 'listBox1 empty. Set the 'Sorted Property of both ListBoxes to 'true.
// number of items that can be selected
private const int selectedItemLimit = 5;

 private object currentItem;

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
 {
     Console.WriteLine("listBox1 index changed: " + listBox1.SelectedIndex.ToString());

     currentItem = listBox1.SelectedItem;

     if (currentItem == null) return;

    // this is left for you to implement
    // remove the selected item from listBox1
    // add the selected item to listBox1
 }

 private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
 {
     Console.WriteLine("listBox2 index changed: " + listBox1.SelectedIndex.ToString());

     currentItem = listBox2.SelectedItem;

     if (currentItem == null) return;

     if (listBox1.Items.Count < selectedItemLimit)
     {
        // this is left for you to implement
        // add the selected item to listBox1
        // remove the selected item from listBox2
     }
 }

当你工作使用此代码,并且可能扩展它,确保在开发时检查输出到控制台。

As you work with this code, and, possibly, extend it, be sure and examine the output to the Console as you are developing.


使用BillWoodruff的帮助。我使用此代码在新的项目中传输项目dialogform并填写一个checkedlistbox



With BillWoodruff's help.I use this code to transfer items in a new dialogform and fill a checkedlistbox

 private void button6_Click(object sender, EventArgs e)
        {
Sub sub = new Sub();
            sub.GetHomelist = Homelist;
            
            for (int i = 0; i < Homelist.Count; i++)
            {
                sub.checkedListBox1.Items.Add(Homelist[i].Value.ToString());
            }
            sub.checkedListBox1.DisplayMember = "Value";
            this.Opacity = 0.7;     
            sub.ShowDialog();
}





我使用子表单获取checkedlistbox中已检查项目的位置并生成一个新的密钥绑定列表首先按排序顺序排列检查项的值对。





and I use the subform to get the places of the checked items in checkedlistbox and generate a new bindinglist of key value pairs that has the checked items first in sort order.

public partial class Sub : Form
    {
        private BindingList<keyvaluepair><int,>> Homelist;
        public BindingList<keyvaluepair><int,>> GetHomelist
        {
            get { return Homelist; }
            set { Homelist = value; }            
        }        
        public Sub()
        {
            InitializeComponent();            
        }
        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            CheckedListBox items = (CheckedListBox)sender;
            if (items.CheckedItems.Count > 4)
            {
                e.NewValue = CheckState.Unchecked;
            }
        }       
        private void button2_Click(object sender, EventArgs e)
        {
            if (checkedListBox1.CheckedItems.Count == 5)
            {
                BindingList<int> indexes = new BindingList<int>();
                foreach (int indexChecked in checkedListBox1.CheckedIndices)
                {
                    indexes.Add(indexChecked);
                }
                Game game = new Game();

                
                foreach (int PlayingInd in indexes)
                {
                    Homelist.Insert(0, Homelist[PlayingInd]);
                    Homelist.RemoveAt(PlayingInd + 1);
                }
                game.GetHomelist = Homelist;                
                this.Close();
            }
            else { MessageBox.Show("please choose 5 items"); }
        }        
    }


这篇关于使用ShowDialog表单中的checkedlistbox对列表框进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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