0

我有以下代码:

Dim widthS = 792
Dim setW = Me.Controls("TableLayoutPanel" + CStr(x))
setW.Width = widthS - 26

但是每当我尝试时,我都会遇到异常"Object reference not set to an instance of an object.",我尝试过DirectCast()但没有效果,有人有解决方案吗?
谢谢你。

4

1 回答 1

2

You need to actually declare the variable types in the Dim statement. For VBA, which you labeled this, it could look like this:

Dim widthS as Long
Dim setW as Control

Set setW = Me.Controls("TableLayoutPanel" + CStr(x))
widthS = 792
setW.Width = widthS - 26

EDIT

It sounds like you are using VB.Net. The basic premise is the same, but in VB.Net you can combine the variable declaration and instantiation like:

Dim widthS as Long = 792
于 2013-11-04T15:00:15.320 回答