使用Vb.Net在Windows窗体中将String声明为控件时获取null值

时间:2017-02-28 22:46:43

标签: vb.net

我尝试使用此代码来声明一些字符串,该字符串将在我的应用程序表单中充当给定的控件。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim Test12 As Control = Controls("Button1")
    Test12.Text = "Working"
End Sub

但是,我得到Test12的值是'NULL',如下所示。我已确认我的表单包含控件名称为 Button1

enter image description here

1 个答案:

答案 0 :(得分:0)

史蒂夫是对的。 只需将此单词添加到您的代码行中,例如:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Test12 As Control = PanelName.Controls("Button1") 'Edited Line
Test12.Text = "Working"
End Sub

PanelName 是包含您的控件的面板的名称" Button1"