In a timer program I am building, I have multiple entrants added dynamically, this bit is easy. Where I am having trouble, I need to activate a stop watch to time the change over, this happens in one of the dynamically added labels, from the dynamically added button. So the code I have from my timer looks like this, when I run the click event I get this result
System.NullReferenceException: 'Object variable or With block variable not set.'
Public Class Form1
ReadOnly MyForm
Dim ss, tt, vv As Integer
Dim ss1, tt1, vv1, ww1 As Integer
'Dim NewLab1
Dim CarNum
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim NewBut1 = New Button
Dim NewBut2 = New Button
Dim NewLab1 = New Label
Dim NewLab2 = New Label
Dim NewLab3 = New Label
Timer3.Enabled = True
' NewBut1()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label3.Text = Format(ss, "00:") & Format(tt, "00:") & Format(vv, "00")
vv = vv 1
If vv > 59 Then
vv = 0
tt = tt 1
End If
If tt = 2 Then
vv = 0
tt = 0
Label3.Text = "00:00:00"
Timer1.Enabled = False
MessageBox.Show("time ended")
End If
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
Label2.Text = Format(Now, "hh:mm:ss")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
CarNum = InputBox("Add Car Number", "Car Number")
Dim NewBut1 = New Button
Dim NewBut2 = New Button
Dim NewLab1 = New Label
Dim NewLab2 = New Label
Dim NewLab3 = New Label
With NewBut1
.BackColor = Color.Red
.ForeColor = Color.White
.Text = "Car " & CarNum & " Pit IN"
.Location = New Point(12, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded ", 16, FontStyle.Bold)
AddHandler NewBut1.Click, AddressOf NewBut1_Click
End With
With NewBut2
.BackColor = Color.Lime
.ForeColor = Color.Black
.Text = "Car " & CarNum & " Pit OUT"
.Location = New Point(225, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded", 16, FontStyle.Bold)
End With
With NewLab1
.BackColor = Color.Black
.ForeColor = Color.Lime
.Location = New Point(537, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded", 16, FontStyle.Bold)
.TextAlign = ContentAlignment.MiddleCenter
.Text = "Pit Stop Timer"
End With
With NewLab2
.BackColor = Color.Black
.ForeColor = Color.Lime
.Location = New Point(748, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded", 16, FontStyle.Bold)
.TextAlign = ContentAlignment.MiddleCenter
.Text = "Stint Timer"
End With
With NewLab3
.BackColor = Color.Black
.ForeColor = Color.Lime
.Location = New Point(953, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded", 16, FontStyle.Bold)
.TextAlign = ContentAlignment.MiddleCenter
.Text = "Timer 3"
End With
Controls.Add(NewBut1)
Controls.Add(NewBut2)
Controls.Add(NewLab1)
Controls.Add(NewLab2)
Controls.Add(NewLab3)
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Dim NewLab1 As Label
NewLab1.Text = Format(ss1, "00:") & Format(tt1, "00:") & Format(vv1, "00.") & Format(ww1, "00.")
vv1 = vv1 1
If vv1 > 59 Then
vv1 = 0
tt1 = tt1 1
End If
If tt1 = 2 Then
vv1 = 0
tt1 = 0
Label1.Text = "00:00:00.00"
Timer2.Enabled = False
MessageBox.Show("time ended")
End If
End Sub
Private Sub NewBut1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Timer2.enabled = True
End Sub
Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
Timer1.Enabled = True
End Sub
Sub newlab1()
End Sub
End Class
CodePudding user response:
Here's how the code should look:
Sub Main
End Sub
' Define other methods and classes here
Public Class Form1
ReadOnly MyForm As Form
Dim ss, tt, vv As Integer
Dim ss1, tt1, vv1, ww1 As Integer
Dim NewBut1 As Button
Dim NewBut2 As Button
Dim NewLab1 As Label
Dim NewLab2 As Label
Dim NewLab3 As Label
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer3.Enabled = True
' NewBut1()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Label3.Text = Format(ss, "00:") & Format(tt, "00:") & Format(vv, "00")
vv = vv 1
If vv > 59 Then
vv = 0
tt = tt 1
End If
If tt = 2 Then
vv = 0
tt = 0
Label3.Text = "00:00:00"
Timer1.Enabled = False
MessageBox.Show("time ended")
End If
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
Label2.Text = Format(Now, "hh:mm:ss")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
CarNum = InputBox("Add Car Number", "Car Number")
With NewBut1
.BackColor = Color.Red
.ForeColor = Color.White
.Text = "Car " & CarNum & " Pit IN"
.Location = New Point(12, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded ", 16, FontStyle.Bold)
AddHandler NewBut1.Click, AddressOf NewBut1_Click
End With
With NewBut2
.BackColor = Color.Lime
.ForeColor = Color.Black
.Text = "Car " & CarNum & " Pit OUT"
.Location = New Point(225, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded", 16, FontStyle.Bold)
End With
With NewLab1
.BackColor = Color.Black
.ForeColor = Color.Lime
.Location = New Point(537, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded", 16, FontStyle.Bold)
.TextAlign = ContentAlignment.MiddleCenter
.Text = "Pit Stop Timer"
End With
With NewLab2
.BackColor = Color.Black
.ForeColor = Color.Lime
.Location = New Point(748, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded", 16, FontStyle.Bold)
.TextAlign = ContentAlignment.MiddleCenter
.Text = "Stint Timer"
End With
With NewLab3
.BackColor = Color.Black
.ForeColor = Color.Lime
.Location = New Point(953, 200)
.Height = 60
.Width = 188
.Font = New Font("Arial Rounded", 16, FontStyle.Bold)
.TextAlign = ContentAlignment.MiddleCenter
.Text = "Timer 3"
End With
Controls.Add(NewBut1)
Controls.Add(NewBut2)
Controls.Add(NewLab1)
Controls.Add(NewLab2)
Controls.Add(NewLab3)
End Sub
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
NewLab1.Text = Format(ss1, "00:") & Format(tt1, "00:") & Format(vv1, "00.") & Format(ww1, "00.")
vv1 = vv1 1
If vv1 > 59 Then
vv1 = 0
tt1 = tt1 1
End If
If tt1 = 2 Then
vv1 = 0
tt1 = 0
Label1.Text = "00:00:00.00"
Timer2.Enabled = False
MessageBox.Show("time ended")
End If
End Sub
Private Sub NewBut1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Timer2.enabled = True
End Sub
Private Sub Button21_Click(sender As Object, e As EventArgs) Handles Button21.Click
Timer1.Enabled = True
End Sub
End Class
Note there are class-level references to the dynamic elements you're creating.
Keep in mind that you probably shouldn't be creating those items everytime you click Button2
.