[英]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. 在开发Windows窗体应用程序时,我有两个列表框和一个移动按钮。
ListBox1
contains strings and ListBox2
is empty. ListBox1
包含字符串,而ListBox2
为空。 Whenever I'm pressing the move button, the selected item in ListBox1
should be moved to ListBox2
. 每当我按下“移动”按钮时,
ListBox1
的选定项都应移至ListBox2
。
I'm getting the default focus on first item on ListBox1
? 我正在默认关注
ListBox1
第一项吗? How can I change the focus to the very next element of the element moved to ListBox2
? 如何将焦点更改为移到
ListBox2
的元素的下一个元素?
Do you mean the element that you just moved to listbox2? 您是说您刚刚移到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. 同意这不是很清楚,无论如何,如果他是指ListBox2中的下一个元素,它将是null,我想是因为他只是在ListBox2项目的末尾添加了该项目。
But the next element in the ListBox1, would be selected by: 但是,ListBox1中的下一个元素将通过以下方式选择:
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. 但是请记住检查indexOfItemMoved是否有任何项目,否则您将遇到异常。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.