Move or Drap and Drop items in CheckedListBox VS.Net 2005

Dear all,

My solution have a form with checkedlistbox and two button (Move Up, Move Down). I want to move up/down items in checkedlistbox when i push buttons up or down or drag and drop items possible.

This is my code :

private void btnMoveUp_Click(object sender, EventArgs e)
{
chkListBox.SelectedIndex -= 1;
}

private void btnMoveDown_Click(object sender, EventArgs e)
{
chkListBox.SelectedIndex += 1;
}

please help me add code to move.
Thanks for cooperation.

Khiem Vo.



Answer this question

Move or Drap and Drop items in CheckedListBox VS.Net 2005

  • Demian Schnaidman

    Dear James Kovacs,

    Your code is helpful I got it, however i would like flexible drag and drop items. Please help me again.

    Thanks for your help. I looking forward to hearing from u.
    Khiem Vo.

  • lgbjr

    Changing the SelectedIndex will change which item is selected, it will not move the selected item up or down in the list. Here's some code to show you how to do that:

            private void btnUp_Click(object sender, EventArgs e) {
                int index = clbItems.SelectedIndex;
                if(index > 0) {
                    MoveItem(-1);
                }
            }
     
            private void btnDown_Click(object sender, EventArgs e) {
                int index = clbItems.SelectedIndex;
                if(index < clbItems.Items.Count - 1) {
                    MoveItem(1);
                }
            }
     
            private void MoveItem(int step) {
                int index = clbItems.SelectedIndex;
                object item = clbItems.SelectedItem;
                // Checked state is lost when you remove and re-insert the item
                bool isChecked = clbItems.GetItemChecked(index);
                clbItems.Items.Remove(item);
                clbItems.Items.Insert(index + step, item);
                clbItems.SetItemChecked(index + step, isChecked);
            }


  • Move or Drap and Drop items in CheckedListBox VS.Net 2005