如何在 VB 的 运行 时间创建文本框时停止 'Object reference not set to an instance of an object'
How to stop an 'Object reference not set to an instance of an object' when creating textboxes at run time in VB
对于一个项目,我需要在 运行 时创建文本框,为此我使用了这段代码;
Dim AOP As Integer = GlobalVariables.AOP
Dim tb(11, 11) As TextBox
Dim LocationX As Integer
Dim LocationY As Integer
Dim count As Integer = 2
LocationX = 10
LocationY = 10
tb(1, 0).Name = "txtRoot"
tb(1, 0).Size = New Size(170, 20)
tb(1, 0).Location = New Point(LocationX, LocationY)
tb(1, 0).Visible = False
然后我可以使用这个 For 循环循环它;
For i = 1 To AOP
If count > AOP Then
Else
If i = AOP Then
LocationY = LocationY + 10
LocationX = 10
tb(count, 0).Name = "txtXValue" & 0 & "YValue" & count
tb(count, 0).Size = New Size(170, 20)
tb(count, 0).Location = New Point(LocationX, LocationY)
Controls.Add(tb(count, 0))
count = count + 1
i = 1
Else
LocationX = LocationX + 10
tb(count, i).Name = "txtXValue" & i & "YValue" & count
tb(count, i).Size = New Size(170, 20)
tb(count, i).Location = New Point(LocationX, LocationY)
Controls.Add(tb(count, i))
End If
End If
Next
这在理论上是可行的,但是,当代码到达该行时;
tb(1, 0).Name = "txtRoot"
它returns错误'Object reference not set to an instance of an object'
我想知道这周围是否有任何东西?或者如果这种创建文本框的方式不可行?任何帮助将不胜感激。
您已初始化数组但未添加初始化TextBoxes
,该数组目前仅包含Nothing
。另请注意,数组是从零开始的,因此第一个 TextBox
在 tb(0, 0)
.
中
For i As Int32 = 0 To tb.GetLength(0) - 1
For ii As Int32 = 0 To tb.GetLength(1) - 1
tb(i, ii) = New TextBox()
tb(i, ii).Visible = False
' .... '
Next
Next
现在都初始化好了。
对于一个项目,我需要在 运行 时创建文本框,为此我使用了这段代码;
Dim AOP As Integer = GlobalVariables.AOP
Dim tb(11, 11) As TextBox
Dim LocationX As Integer
Dim LocationY As Integer
Dim count As Integer = 2
LocationX = 10
LocationY = 10
tb(1, 0).Name = "txtRoot"
tb(1, 0).Size = New Size(170, 20)
tb(1, 0).Location = New Point(LocationX, LocationY)
tb(1, 0).Visible = False
然后我可以使用这个 For 循环循环它;
For i = 1 To AOP
If count > AOP Then
Else
If i = AOP Then
LocationY = LocationY + 10
LocationX = 10
tb(count, 0).Name = "txtXValue" & 0 & "YValue" & count
tb(count, 0).Size = New Size(170, 20)
tb(count, 0).Location = New Point(LocationX, LocationY)
Controls.Add(tb(count, 0))
count = count + 1
i = 1
Else
LocationX = LocationX + 10
tb(count, i).Name = "txtXValue" & i & "YValue" & count
tb(count, i).Size = New Size(170, 20)
tb(count, i).Location = New Point(LocationX, LocationY)
Controls.Add(tb(count, i))
End If
End If
Next
这在理论上是可行的,但是,当代码到达该行时;
tb(1, 0).Name = "txtRoot"
它returns错误'Object reference not set to an instance of an object' 我想知道这周围是否有任何东西?或者如果这种创建文本框的方式不可行?任何帮助将不胜感激。
您已初始化数组但未添加初始化TextBoxes
,该数组目前仅包含Nothing
。另请注意,数组是从零开始的,因此第一个 TextBox
在 tb(0, 0)
.
For i As Int32 = 0 To tb.GetLength(0) - 1
For ii As Int32 = 0 To tb.GetLength(1) - 1
tb(i, ii) = New TextBox()
tb(i, ii).Visible = False
' .... '
Next
Next
现在都初始化好了。