开发者

Focus To Next Item in ListBox in Window Form Application

开发者 https://www.devze.com 2023-03-22 19:26 出处:网络
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 L

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 mov开发者_Go百科ed 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?


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号