简体   繁体   中英

Trying to add UserControl to a TabPage inside of another TabPage get NullReferenceException

I have an UserControl and wish to add it to a TabPage Inside another TabPage but get NullReferenceException instead.

My code

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

The IDE say that I must declare it using the word "New", but I already did it on the first code line.

Another thing, If I iterate this code changing the name and coordinates I will get independent controls or when I change one all do the same?

My form looks like this.

标签内的标签示例

Thanks to LarsTech I solve the problem and add several UserControls to my TabPage using the following code

    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

I hope someone finds this useful; bye.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM