user829222
user829222

Reputation: 75

Focus To Next Item in ListBox in Window Form Application

While developing a Windows forms application , I'm having two ListBoxs and a move button. ListBox1 contains strings and ListBox2 is empty. Whenever I'm pressing the move button, the selected item in ListBox1 should be moved to ListBox2.

I'm getting the default focus on first item on ListBox1? How can I change the focus to the very next element of the element moved to ListBox2?

Upvotes: 0

Views: 989

Answers (2)

Tafari
Tafari

Reputation: 3069

Agree it is not very clear, anyway if he meant the next element in the ListBox2 it would be null I guess as he just added the item at the end of the ListBox2 items.

But the next element in the ListBox1, would be selected by:

private void Move()
    {
        indexOfItemMoved = listBox1.SelectedIndex;

        //move operation

        listBox1.SelectedIndex = indexOfItemMoved; //or indexOfItemMoved+1 / -1 depending on the move operation
    }

But remember to check if there is any item at the indexOfItemMoved or you will encounter an exception.

Upvotes: 0

LarsTech
LarsTech

Reputation: 81620

Do you mean the element that you just moved to listbox2? It's not very clear.

After moving the item, you could just:

listBox2.SelectedIndex = listBox2.Items.Count - 1;

Upvotes: 1

Related Questions