Dear All,


I am using 2 listboxes, 1st containing values and 2nd to hold values selected from 1st listbox. On double clicking in 1st listbox selected value moves to 2nd listbox.
Requirment is after movment of selected value to 2nd listbox countrol should be back in 1st listbox and next or previous item (of moved value)should be selected unless their remain some item in 1st listbox.

Kindly guide,

Yawer

Dear All,

I am using 2 listboxes, 1st containing values and 2nd to hold values selected from 1st listbox. On double clicking in 1st listbox selected value moves to 2nd listbox.
Requirment is after movment of selected value to 2nd listbox countrol should be back in 1st listbox and next or previous item (of moved value)should be selected unless their remain some item in 1st listbox.

Kindly guide,

Yawer

Try following..

private void btnMove_Click(object sender, EventArgs e)
    {
        string strValue = (string)listBox1.SelectedItem; //Get Selected Item.
        int nSelected   = listBox1.SelectedIndex;  //Get Selected Item Index.

        //Check if selected item is last index (count - 1) position.
        if ((nSelected == listBox1.Items.Count - 1) &&
             (listBox1.Items.Count > 1) )
        {

            listBox1.SetSelected( (nSelected - 1) , true);
        }

        //else item count is greater than 1.
        else if (listBox1.Items.Count > 1)
        {
            listBox1.SetSelected( (nSelected + 1) , true);
        }

        //Remove Selected Item.
        if (nSelected >= 0)
        {
            listBox1.Items.RemoveAt(nSelected);

            listBox2.Items.Add(strValue);
            listBox2.Update();
        }
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.