简体   繁体   English

如何在循环中逐个字符串显示 label 上的字符串数组?

[英]How to display array of strings on a label in string by string in a loop?

This code is now in the form1 constructor.此代码现在位于 form1 构造函数中。

if (filesRadar.Length > 0)
                            {
                                for (int i = 0; i < filesRadar.Length; i++)
                                {
                                    label2.Text = dates[i].ToString("ddd, dd MMM yyy HH':'mm");
                                }
                            }

I want to display the strings in dates when calling a method or something in the constructor and also in other places like when downloading finished.我想在调用构造函数中的方法或其他内容时以及在下载完成时等其他地方显示日期字符串。 each time calling a method or enabling timer to display the strings in a loop in specific speed.每次调用方法或启用计时器以特定速度循环显示字符串。

Now it's displaying but too fast then in the end displaying the last item only and it's not in loop.现在它正在显示,但太快了,最后只显示最后一项并且它不在循环中。

You can make this as a void method and you can call whenever you want i guess.你可以把它作为一个 void 方法,你可以随时调用我猜。

    public void ReportTime()
    {
            for (int i = 0; i < filesRadar.Length; i++)
                label2.Text += dates[i].ToString("ddd, dd MMM yyy HH':'mm") + Environment.NewLine;
    }
    

or enabling timer to display the strings in a loop in specific speed.或启用计时器以特定速度循环显示字符串。

What's keeping you from doing that?...是什么阻止你这样做?...

Drop the timer on your form and set its Interval property to something reasonable.将计时器放在表单上并将其Interval属性设置为合理的值。 For instance, an interval of 1000 would mean it changes every second.例如,1000 的间隔意味着它每秒都在变化。 Make sure the Timer is turned on (Enabled).确保定时器已打开(启用)。

Now move the i variable out to class level and get rid of the for loop:现在将i变量移出到 class 级别并摆脱for循环:

private int i = -1;

private void timer1_Tick(object sender, EventArgs e)
{
    if (filesRadar.Length > 0)
    {
        i++;
        if (i >= filesRadar.Length)
        {
            i = 0;
        }                
        label2.Text = dates[i].ToString("ddd, dd MMM yyy HH':'mm");                
    }
    else
    {
        label2.Text = ""; // optional?
    }
}

You do not need the if-statement , because the loop will only execute if there are items in the array:您不需要if-statement ,因为只有在数组中有项目时才会执行循环:

for (int i = 0; i < filesRadar.Length; i++)
{
    label2.Text += dates[i].ToString("ddd, dd MMM yyy HH':'mm") + Enviroment.NewLine;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM