如何在 vb.net 中更新标签文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6848495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:45:54  来源:igfitidea点击:

How to update label text in vb.net

.netvb.net

提问by Ram

In my vb.net winform application, on click of start button the label1.text should be "process started" then some filesaving method will run after finish that method the label1.text should change to "file saved".

在我的 vb.net winform 应用程序中,单击开始按钮,label1.text 应该是“进程开始”,然后在完成该方法后将运行一些文件保存方法,label1.text 应该更改为“文件已保存”。

 Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

    lblStatus.ForeColor = Color.Red
    lblStatus.Text = "Saving to File"

     'Get the values and write to xls
             Trigger()
             SaveXls()

     lblStatus.Text = "File Saved"
     lblStatus.ForeColor = Color.Green
End Sub

the initial status of label "saving to file" is not comingup. after the Trigger method finished, the status of the label is changing to "File saved"

标签“保存到文件”的初始状态没有出现。触发方法完成后,标签状态变为“文件已保存”

Any suggestions please?

请问有什么建议吗?

回答by Alan McBee - MSFT

You need to use the Refresh() method of the label. Using Application.DoEvents has side effects and should be used carefully (this is not the appropriate use for it).

您需要使用标签的 Refresh() 方法。使用 Application.DoEvents 有副作用,应谨慎使用(这不是它的适当用途)。

回答by Dmitry Selitskiy

Alternative to the other two answers (and my preference) would be to use Background Workerto execute Trigger()and SaveXls().

其他两个答案(和我的偏好)的替代方法是使用后台工作人员执行Trigger()SaveXls().

Your code will look something like:

您的代码将类似于:

Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click

    lblStatus.ForeColor = Color.Red
    lblStatus.Text = "Saving to File"

    If backgroundWorker1.IsBusy <> True Then
        ' Start the asynchronous operation.
        backgroundWorker1.RunWorkerAsync()
    End If
End Sub

Private Sub backgroundWorker1_DoWork(ByVal sender As System.Object, _
ByVal e As DoWorkEventArgs) Handles backgroundWorker1.DoWork
     'Get the values and write to xls
             Trigger()
             SaveXls()
End Sub

Private Sub backgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, _
ByVal e As RunWorkerCompletedEventArgs) Handles backgroundWorker1.RunWorkerCompleted
    If e.Cancelled = True Then
        lblStatus.Text = "Canceled!"
        lblStatus.ForeColor = Color.Black
    ElseIf e.Error IsNot Nothing Then
        lblStatus.Text = "Error: " & e.Error.Message
    Else
        lblStatus.Text = "File Saved"
        lblStatus.ForeColor = Color.Green
    End If
End Sub

Using Background Worker will also leave your form responsive while the background operation is happening instead of freezing it.

使用后台工作器还会使您的表单在后台操作发生时保持响应,而不是冻结它。

回答by Ram

after set the label text initially, refresh the form using form1.refresh(). Then Trigger() and SaveXls() functions will execute and finally change the label text to "filesaved".

最初设置标签文本后,使用 form1.refresh() 刷新表单。然后 Trigger() 和 SaveXls() 函数将执行并最终将标签文本更改为“filesaved”。

Thanks for all ur replies and efforts

感谢您的所有回复和努力

回答by George Johnston

You'll need to quit blocking your code for a moment after you update your lblStatus label. You can try putting an Application.DoEventsafter your first label update. Otherwise, your form will wait to refresh until your entire code block has finished executing. Application.DoEventswill suspend your current thread, process the windows messages, and then continue executing once finished.

更新 lblStatus 标签后,您需要暂时停止阻止代码。您可以尝试Application.DoEvents在第一次标签更新后放置一个。否则,您的表单将等待刷新,直到您的整个代码块执行完毕。 Application.DoEvents将挂起当前线程,处理 Windows 消息,然后在完成后继续执行。