注册 登录
编程论坛 VB.NET论坛

VB.Net 声明全局变量a=PictureBox1 之后为何不能使用?

Antoom 发布于 2016-10-28 23:28, 2933 次点击
程序关键部分简化如下:
Public Class Form1
    Dim a As PictureBox = PictureBox1

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        a.Image = Image.FromFile(Application.StartupPath & "\1.jpg")
    End Sub
End Class
结果显示“未将对象引用设置到对象的实例”,图片不能导入。
为什么会出现这种情况?
如何解决?(保留全局变量的声明)

1 回复
#2
梦幻倩影2016-10-29 10:18
Public Class Form1
 Public a As New PictureBox  '定义公共变量
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        a = Me.PictureBox1
        a.Image = Image.FromFile(Application.StartupPath & "\1.jpg")
    End Sub
End Class
1