更改文本标签

本文关键字:文本标签 | 更新日期: 2023-09-27 18:06:55

如何在asp.net visual basic中运行程序或函数时更改文本标签。例如,当项目正在验证某些东西时,标签也会这样说。(对不起,我不会说英语)

更改文本标签

我认为你想要做的是:-有一种状态标签与变化的文本,这取决于当前的任务-在应用程序线程上执行过程/函数(例如通过点击按钮)

如何处理?: 我的做法如下:要求:一个定时器附加到你的表单-一个按钮和一个标签(仅为我的例子)

下面是我的方法的代码:

private String currentStatus = "Idle";
    /*
    *   Use this while working with Lists or other kinds of arrays
    *   private object syncObject = new object();
    */
    private void button1_Click(object sender, EventArgs e)
    {
        // Keep in mind that you should disable the button while the thread is running
        new Thread(new ThreadStart(DoTask)).Start();
    }
    private void DoTask()
    {
        /*
        *   If you are working with Lists for example
        *   you should use a lock to prevent modifications
        *   while actually iterating the list.
        *   Thats how you use it:
        *   lock(syncObject){// You can do it for a single or a bunch of list actions
        *       list.Add(item); 
        *   }
        */
        currentStatus = "Waiting...";
        Thread.Sleep(1000);
        currentStatus = "Scanning...";
        Thread.Sleep(1000);
        currentStatus = "Extracting data...";
        Thread.Sleep(1000);
        currentStatus = "Done!";
    }
    private void tickTimer_Tick(object sender, EventArgs e)
    {
        statusLabel.Text = currentStatus;
    }

请记住:您不能更改任何控件值,如标签文本或其他线程!这就是我使用String字段的原因。

我希望这对你有帮助。