尝试将UserControl添加到另一个TabPage内的TabPage获取NullReferenceException

时间:2014-04-04 14:30:52

标签: vb.net user-controls add vb.net-2010 tabpage

我有一个UserControl并希望将它添加到TabPage里面另一个TabPage但是得到NullReferenceException。

我的代码

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim MyControl As New TimerPanel
    Dim Ubicacion As Point
    Ubicacion.X = 274
    Ubicacion.Y = 100
    With MyControl
        .Name = "Timer0"
        .NombreTimerTxt.Text = "Timer GPS"
        .TimerBox.Text = "Timer 00"
        .Parent = Me '.TabControl3.TabPages("Timers")
        .Location = Ubicacion
        .Visible = True

    End With

    Me.TabControl3.TabPages("Timers").Controls.Add(MyControl) 'Error here

    'Me.TabControl1.TabPages("Timers").Controls("Timer0").Location = Ubicacion
End Sub

IDE说我必须使用“New”这个词来声明它,但我已经在第一个代码行上做了。

另一件事,如果我迭代这段代码改变名称和坐标我将获得独立控制或当我改变一个时都做同样的事情?

我的表单看起来像这样。

Tab inside tab example

1 个答案:

答案 0 :(得分:0)

感谢LarsTech我解决了这个问题,并使用以下代码将多个UserControl添加到我的TabPage

    Dim X As Integer = 4
    Dim Y As Integer = 0

    For XTimer As Integer = 0 To 15
        Dim MyControl As New TimerPanel
        Dim Ubicacion As Point
        Ubicacion.X = X
        Ubicacion.Y = Y
        With MyControl
            .Name = "Timer" & XTimer.ToString
            .NombreTimerTxt.Text = "Timer GPS"
            .TimerBox.Text = "Timer " & XTimer.ToString
            .Parent = Me
            .Location = Ubicacion
            .Visible = True
        End With
        TimersTab.Controls.Add(MyControl)
        Y += 51

        If XTimer = 9 Then 'Start column 2
            X = 344
            Y = 0
        End If
    Next

我希望有人觉得这很有用;再见。

相关问题