我想在列表框控件中重复(当它到达最后一个时,重复)循环并将文本设置为标签。
我卡住了,请帮忙!
最佳答案
不确定你要达到什么目的,但是下面的方法会不断循环给定ListBox的项目,显示给定Label控件中的值,到达结束时从头开始返回,刷新两次第二(C# 代码):
private int _currentIndex = -1;
private void ShowNextItem(ListBox listBox, Label label)
{
// advance the current index one step, and reset it to 0 if it
// is beyond the number of items in the list
_currentIndex++;
if (_currentIndex >= listBox.Items.Count)
{
_currentIndex = 0;
}
label.Text = listBox.Items[_currentIndex].ToString();
// get a thread from the thread pool that waits around for a given
// time and then calls this method again
ThreadPool.QueueUserWorkItem((state) =>
{
Thread.Sleep(500);
this.Invoke(new Action<ListBox, Label>(ShowNextItem), listBox, label);
});
}
这样调用它:
ShowNextItem(myListBox, myLabel);
关于c# - 在 VB.net 或 C# 中循环列表框并将值设置为标签文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1004171/