VB.net标签未刷新(Me.Refresh())

VB.net标签未刷新(Me.Refresh()),.net,vb.net,.net,Vb.net,我正在使用VB.net创建一个应用程序,我以前从未使用过VB.net,所以我可能不知道我在做什么 我的问题是我的表单不会刷新标签,我尝试过使用Me.refresh()、Me.Update(),我尝试过在刷新之前使控件无效,我甚至尝试过Application.DoEvents()(尽管我知道不建议这样做) 这里有两个代码段,一个显示代码中变量被更改的位置,另一个显示变量显示的标签 Private Sub Label1_Click(sender As Object, e As System.Wind

我正在使用VB.net创建一个应用程序,我以前从未使用过VB.net,所以我可能不知道我在做什么

我的问题是我的表单不会刷新标签,我尝试过使用Me.refresh()、Me.Update(),我尝试过在刷新之前使控件无效,我甚至尝试过Application.DoEvents()(尽管我知道不建议这样做)

这里有两个代码段,一个显示代码中变量被更改的位置,另一个显示变量显示的标签

Private Sub Label1_Click(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Label1.Click
    If e.Button = Windows.Forms.MouseButtons.Right Then
        ContextMenuStrip1.Show(CType(sender, Control), e.Location)
    End If
    Functionality.selectedTask = 0
    Me.Refresh()
End Sub
单击此(Label1)时,Label11应更新(Label1表示任务0)


这就是我测试Label11是否更新的方式(在最终的程序中,这应该显示所选任务的描述)。它在15时以所选任务开始,但不会更改。我确信某些变量本身正在变化,但这种变化并没有表现出来。

下面是一个简短的示例,说明如何使用
按钮更改
标签的文本。单击
事件。您应该能够将其复制粘贴到新的VB.NET Windows窗体应用程序中:

Option Strict On
Option Explicit On


Public Class Form1

    Private WithEvents newLabel As New System.Windows.Forms.Label
    Private WithEvents newButton As New System.Windows.Forms.Button

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        newLabel.Location = New Point(10, 10)
        newLabel.Text = "Orignial value"

        newButton.Location = New Point(10, 40)
        newButton.AutoSize = True
        newButton.Text = "Change the label"

        Me.Controls.Add(newLabel)
        Me.Controls.Add(newButton)

    End Sub


    Public Sub changeTheLabel(sender As Object, e As EventArgs) Handles newButton.Click
        Me.newLabel.Text = "Text Changed"
    End Sub
End Class

Me.Label11.Text=Convert.ToString(Functionality.selectedTask)
不在单击事件中-它应该如何执行此操作。这段代码在哪里?请回复OneFineDay:它在程序的一个单独的部分中--它在Form1.Designer.vb中,在那里初始化变量。然后它将永远不会更改。无论如何,您不应该更改该文件中的代码。将第二个代码段放在第一个代码段中,替换为Me.Refresh()。忽略大部分代码:Label.Text=“Text Changed”将更新标签。@rheitzman,您说得对,
Me.newLabel.Text=“Text Changed”
只要调用线程拥有标签控件,代码位实际上就会更改标签的文本。但是,这个问题特别指出,当单击另一个控件时,标签应该更新,上面的代码是一个简短、完整且可验证的解决方案,显示了请求的行为。
Option Strict On
Option Explicit On


Public Class Form1

    Private WithEvents newLabel As New System.Windows.Forms.Label
    Private WithEvents newButton As New System.Windows.Forms.Button

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        newLabel.Location = New Point(10, 10)
        newLabel.Text = "Orignial value"

        newButton.Location = New Point(10, 40)
        newButton.AutoSize = True
        newButton.Text = "Change the label"

        Me.Controls.Add(newLabel)
        Me.Controls.Add(newButton)

    End Sub


    Public Sub changeTheLabel(sender As Object, e As EventArgs) Handles newButton.Click
        Me.newLabel.Text = "Text Changed"
    End Sub
End Class