none
how to select next value in listbox after moving a value from listbox?

    Question

  • 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

    • Edited by Haansi Monday, August 11, 2008 4:22 PM Thanks.
    Monday, August 11, 2008 12:48 PM

Answers

  • Try this:

            private void listBox1_DoubleClick(object sender, EventArgs e) 
            { 
                string s = listBox1.SelectedItem.ToString(); 
                int selectedIndex = listBox1.SelectedIndex; 
                int newSelectedIndex = (selectedIndex > 0) ? selectedIndex : 0; 
                listBox2.Items.Add(s); 
                listBox1.Items.RemoveAt(selectedIndex); 
                if (listBox1.Items.Count > 0) 
                    listBox1.SelectedIndex = newSelectedIndex; 
            } 

    Take care of all scenarios (operations involving indices) to avoid any exception at runtime.
    • Marked as answer by Haansi Tuesday, August 12, 2008 2:40 AM
    • Marked as answer by Haansi Tuesday, August 12, 2008 2:40 AM
    • Marked as answer by Haansi Tuesday, August 12, 2008 2:40 AM
    Monday, August 11, 2008 5:30 PM

All replies

  • Try this:

            private void listBox1_DoubleClick(object sender, EventArgs e) 
            { 
                string s = listBox1.SelectedItem.ToString(); 
                int selectedIndex = listBox1.SelectedIndex; 
                int newSelectedIndex = (selectedIndex > 0) ? selectedIndex : 0; 
                listBox2.Items.Add(s); 
                listBox1.Items.RemoveAt(selectedIndex); 
                if (listBox1.Items.Count > 0) 
                    listBox1.SelectedIndex = newSelectedIndex; 
            } 

    Take care of all scenarios (operations involving indices) to avoid any exception at runtime.
    • Marked as answer by Haansi Tuesday, August 12, 2008 2:40 AM
    • Marked as answer by Haansi Tuesday, August 12, 2008 2:40 AM
    • Marked as answer by Haansi Tuesday, August 12, 2008 2:40 AM
    Monday, August 11, 2008 5:30 PM
  • Thanks a lot Jayanta Dey for guiding me.

    Thakns again.

    • Marked as answer by Haansi Tuesday, August 12, 2008 2:40 AM
    • Unmarked as answer by Haansi Tuesday, August 12, 2008 2:40 AM
    Tuesday, August 12, 2008 2:39 AM