如何从另一种形式的visual basic中更改标签中的文本?

时间:2015-10-18 05:22:42

标签: vb.net

Public Class Form1
    Dim NewMDIChild1 As New Form2()
    Dim NewMDIChild2 As New Form3()

    Private Sub Form1ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Form1ToolStripMenuItem.Click
        NewMDIChild1 = New Form2
        NewMDIChild1.MdiParent = Me
        NewMDIChild1.Show()
    End Sub

    Private Sub Form2ToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles Form2ToolStripMenuItem.Click
        NewMDIChild2 = New Form3
        NewMDIChild2.MdiParent = Me
        NewMDIChild2.Show()
    End Sub

    Private Sub CloseToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseToolStripMenuItem.Click
        If Not ActiveMdiChild Is Nothing Then
            ActiveMdiChild.Close()
        End If
    End Sub

    Private Sub CloseTodoToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CloseTodoToolStripMenuItem.Click
        While Not ActiveMdiChild Is Nothing
            ActiveMdiChild.Close()
        End While
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub SmallToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SmallToolStripMenuItem.Click
Form2.Label1.Text = "test"
    End Sub

    Private Sub MediumToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MediumToolStripMenuItem.Click
Form2.Label1.Text = "test"
    End Sub

    Private Sub BigToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles BigToolStripMenuItem.Click
Form2.Label1.Text = "test"
    End Sub
End Class

我想要做的是改变标签的大小,问题是标签是另一种形式,它似乎没有改变。我正在使用这个" Form2.Label1.Text =" test""只是看看标签是否改变了。我认为这是因为我正在使用MDIChild。有谁可以帮助我?

1 个答案:

答案 0 :(得分:2)

您没有显示与您的问题相关的代码,无论您在哪里更改任何标签上的任何文字,因此我将忽略99%的巨大代码块,并将您的问题归结为:

Dim NewMDIChild1 As New Form2()
Dim NewMDIChild2 As New Form3()
     

我使用的是“Form2.Label1.Text =”test“”

我不确定VB.NET的行为是否与VB6 / VBA相同,但是如果你没有得到Form2.Label1.Text = "test"的异常,那么可能就是这种情况 - 你正在设置文本表单的默认实例上的标签,并且您的类Form1使用名为Form2的对象引用引用NewMDIChild1

根据您拥有该测试代码的位置,执行NewMDIChild1.Label1.Text = "text"应该会产生预期的结果。