c# - 在 VB.net 或 C# 中循环列表框并将值设置为标签文本?

标签 c# vb.net loops listbox

我想在列表框控件中重复(当它到达最后一个时,重复)循环并将文本设置为标签。

我卡住了,请帮忙!

最佳答案

不确定你要达到什么目的,但是下面的方法会不断循环给定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/

相关文章:

vb.net - DbFunctions.TruncateTime 在不同服务器时区的行为是否不同?

vb.net - Sub 的关闭表格

xml - 如何将变量插入到 xml 文字注释中

java - 程序只循环一次但应该循环T次

c# - 从 asp.net 中的网页生成 Pdf

c# - ASP.NET Core 服务不会重定向 http :localhost:<https-port> to https schema

c# - "Cannot be accessed with an instance reference; qualify it with a type name instead"

c# - 具有 volatile 或内存屏障的双锁

python - C++ 中的循环 (while)

android - 使用 Android 的 AudioTrack