Click here to Skip to main content
15,940,921 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
I fill a listbox "listBoxHome" from a list "uList" :

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

I also have a button "CHANGE". When I push button CHANGE I use Showdialog to open a new form called Sub
C#
Sub sub = new Sub();
 sub.ShowDialog();


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?
Posted
Updated 31-Mar-14 7:35am
v2
Comments
Sergey Alexandrovich Kryukov 23-Mar-14 12:59pm    
The problem is that you keep the control bound to some object for which sorting might not even be defined. You should either work unbound and then collect data into some collection object supporting sorting, or bind with something sortable. Your idea of UI design looks... questionable...
—SA
BillWoodruff 23-Mar-14 13:05pm    
Why not have one CheckedListBox, and as the user checks items (up to five), the checked items are moved to the Top, and displayed in sorted order, while the non-checked are also sorted ?
iratus7 23-Mar-14 13:32pm    
Yes this is what I want.Can you help me with the code?

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.
C#
// 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.
 
Share this answer
 
v2
With BillWoodruff's help.I use this code to transfer items in a new dialogform and fill a checkedlistbox

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


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.

C#
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"); }
        }        
    }
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900