字符串数组到标签 [英] String array to a label

查看:112
本文介绍了字符串数组到标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组,我需要将其内容显示在单个标签上.它就像一个时钟.事情是我尽力而为,但失败了.这是我一直在尝试的代码.它在 VB.NET 中,并且运行Thread.Sleep(2000)以减慢循环速度,因此我可以在标签中显示内容2秒钟.那是引起问题的原因吗?请帮忙.
谢谢.

I''ve got a string array and I need to show the contents of it to a single label. It acts like a clock. The thing is I tried with everything I could but I failed. This is the code I''ve been trying. Its in VB.NET and there is a Thread.Sleep(2000) running to slow down the loop so I can display the contents in a label for 2 seconds. Is that the thing causing the problem? Please help.
Thanks.

Dim j As Integer
For j = 0 To readText.Length
    label.text=readText(j)
    Thread.Sleep(2000)
Next



[已从OP的答案"中移至问题]
这是我的代码,仍然无法正常工作:



[Moved to question from an "answer" for OP]
Here is my code it''s still not functioning:

Imports System
Imports System.IO
Imports System.Threading

Public Class Form2
Dim readText() As String

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim path As String = "c:\test.txt"
    readText = File.ReadAllLines(path)

    Dim temp As String
    Dim i As Integer
    Dim rnum As Integer
    Dim r As New Random

    For Each text As String In readText
        rnum = r.Next(0, readText.Length)
        temp = readText(i)
        readText(i) = readText(rnum)
        readText(rnum) = temp
    Next text

    Dim j As Integer
    For j = 0 To readText.Length
        Label1.Text = readText(j)
        Thread.Sleep(2000)
    Next j

End Sub

End Class

推荐答案

使用BackgroundWorker对象控制显示时间,并使用Progress事件根据您发送给事件的进度值显示实际数据.下面的代码在C#中,但是将其转换为vb应该不是问题.

Use a BackgroundWorker object to control the display time, and use the Progress event to display the actual data based on the progress value you sentd to the event. The code below is in C#, but converting it to vb shouldn''t be a problem.

private void worker_DoWork(object sender)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    int delay = 2000;
    int interval = 100;
    int elapsed = 0;
    int pos = myString.Length;

    while (!worker.CancellationPending)
    {
        if (eplapsed >= delay)
        {
            worker.ReportProgress(pos);  // change label text in the Progress event handler
            pos++;
            elapsed = 0;
            if (pos == myString.Length)
            {
                break;
            }
        }
        Thread.Sleep(interval);
    }
}


警告:我在没有测试的情况下输入了此内容,因此您可能需要对其进行一些调整.这种方法还可以使UI响应用户输入,并且避免使用DoEvents().


Caveat: I typed this without testing it so you may need to tweak it a little. This approach keeps your UI responsive to user input as well, and avoids using DoEvents().


如果您是vb.net的新手,以上内容都有些复杂.使用计时器会非常容易.这里永远不会出现线程或睡眠问题.
怎么做:
1.在表单中添加一个计时器.
2.添加到proc Form2_Load
If you are new at vb.net All above are little bit complex. It would be very easy using a Timer. Threading or sleeping problems will never arise here.
How to do that:
1. Add a timer into your form.
2.Add into proc Form2_Load
Timer1.Enabled = True
Timer1.Interval = 2000
Timer1.Tag=0


3.您设置


3. You set

Dim readText() As String

(全局).所以没问题.请勿更改.
4.现在:

as global. So no problem. Do not change it.
4. Now:

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    Try
        Label.Text = readText(Timer1.Tag)
        Timer1.Tag = Timer1.Tag + 1
    Catch ex As Exception
        Timer1.Tag = 0
        Label.Text = readText(Timer1.Tag)
        Timer1.Tag = Timer1.Tag + 1
    End Try
End Sub


计时器存在0〜15ms的容许误差.我不知道为什么可能是我的电脑运行缓慢.


Timer has a problem that is it 0~15ms tolerent. I don''t know why. May be my pc is slow.


Thread.Sleep(2000)正在阻止您的UI线程.在循环运行时,GUI不会响应.要管理长时间运行的方法,您将必须使用BackgroundWorker并从此处更新GUI控件(在这种情况下为标签).为了能够从BackgroundWorker访问控件,您必须使用Invoke();.参见此处: http://blogs.msdn.com/b/csharpfaq/archive /2004/03/17/91685.aspx [
Thread.Sleep(2000) is blocking your UI thread. While the loop is running the GUI will not respond. To manage long running methods you''ll have to use a BackgroundWorker and update your GUI control (label in this case) from there. To be able to access the control from the BackgroundWorker you''ll have to use Invoke();. See here: http://blogs.msdn.com/b/csharpfaq/archive/2004/03/17/91685.aspx[^].

Or just look at JSOP''s answer. That''s the way to do it!

Best Regards,
Manfred


这篇关于字符串数组到标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆