How to address a dynamically added label - vb.net

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

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.

Related

Snake game loses focus and i am unable to move after selected a combo box or pressing a button

For some reason when I added the start button and combobox to try and create profiles the program unfocuses the game inside the picture box and i am unable to press (w,a,s,d) to play it.
If i remove the button and combobox the game works fine. So i think it is some sort of focus issue i just dont know how to fix it, here is my code:
Imports System.IO
Public Class Form1
#Region "snake"
Dim snake(999) As PictureBox
Dim lengthSnake As Integer = -1
Dim leftRightMover As Integer = 0
Dim upDownMover As Integer = 0
Dim random As New Random
Sub createHead()
lengthSnake += 1
snake(lengthSnake) = New PictureBox
With snake(lengthSnake)
.Height = 15
.Width = 15
.BackColor = Color.LimeGreen
.Top = (picPlayingArea.Top + picPlayingArea.Bottom) / 2
.Left = (picPlayingArea.Left + picPlayingArea.Right) / 2
End With
Controls.Add(snake(lengthSnake))
snake(lengthSnake).BringToFront()
snakegrow()
snakegrow()
End Sub
Private Sub Form1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Me.KeyPress
tmrSnakeMove.Start()
Select Case e.KeyChar
Case "a"
leftRightMover = -15
upDownMover = 0
Case "d"
leftRightMover = 15
upDownMover = 0
Case "w"
upDownMover = -15
leftRightMover = 0
Case "s"
upDownMover = 15
leftRightMover = 0
End Select
End Sub
Private Sub tmrSnakeMove_Tick(sender As Object, e As EventArgs) Handles tmrSnakeMove.Tick
For i = lengthSnake To 1 Step -1
snake(i).Top = snake(i - 1).Top
snake(i).Left = snake(i - 1).Left
Next
snake(0).Top += upDownMover
snake(0).Left += leftRightMover
collideWalls()
collideApple()
collideSnake()
End Sub
Sub snakegrow()
lengthSnake += 1
snake(lengthSnake) = New PictureBox
With snake(lengthSnake)
.Height = 15
.Width = 15
.BackColor = Color.Green
.Top = snake(lengthSnake - 1).Top
.Left = snake(lengthSnake - 1).Left + 10
End With
Controls.Add(snake(lengthSnake))
snake(lengthSnake).BringToFront()
End Sub
Sub snakedied()
If Val(lblApple.Text) > Val(lblTrophy.Text) Then
Dim FS As New FileStream("highscore.txt", FileMode.Create, FileAccess.Write)
Dim SW As New StreamWriter(FS)
SW.WriteLine(Score)
SW.Close()
FS.Close()
End If
End Sub
#End Region
#Region "Collision"
Sub collideWalls()
If snake(0).Left < picPlayingArea.Left Then
tmrSnakeMove.Stop()
MsgBox("Game Over")
snakedied()
Me.Close()
ElseIf snake(0).Right > picPlayingArea.Right Then
tmrSnakeMove.Stop()
MsgBox("Game Over")
snakedied()
Me.Close()
ElseIf snake(0).Top < picPlayingArea.Top Then
tmrSnakeMove.Stop()
MsgBox("Game Over")
snakedied()
Me.Close()
ElseIf snake(0).Bottom > picPlayingArea.Bottom Then
tmrSnakeMove.Stop()
MsgBox("Game Over")
snakedied()
Me.Close()
End If
End Sub
Dim Score As Integer
Sub collideApple()
If snake(0).Bounds.IntersectsWith(apple.Bounds) Then
Score = Score + 1
snakegrow()
apple.Top = random.Next(picPlayingArea.Top, picPlayingArea.Bottom - 10)
apple.Left = random.Next(picPlayingArea.Left, picPlayingArea.Right - 10)
lblApple.Text = Score
End If
End Sub
Sub collideSnake()
For i = 1 To lengthSnake
If snake(0).Bounds.IntersectsWith(snake(i).Bounds) Then
tmrSnakeMove.Stop()
MsgBox("Game Over")
snakedied()
Me.Close()
End If
Next
End Sub
#End Region
#Region "Apples"
Dim apple As PictureBox
Sub createApple()
apple = New PictureBox
With apple
.Width = 15
.Height = 15
.BackColor = Color.Red
.Top = random.Next(picPlayingArea.Top, picPlayingArea.Bottom - 10)
.Left = random.Next(picPlayingArea.Left, picPlayingArea.Right - 10)
End With
Controls.Add(apple)
apple.BringToFront()
End Sub
#End Region
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
lblTrophy.Text = IO.File.ReadAllText("highscore.txt")
picApple.Image = My.Resources.apple_icon
picTrophy.Image = My.Resources.trophy
End Sub
Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click
If cmbChoice.Text <> Nothing Then
btnStart.Visible = False
cmbChoice.Enabled = False
createHead()
createApple()
Else
MsgBox("Choose a player.")
End If
End Sub
End Class

Loop through Dates vb.net

I want to create a datagridview every day. Here is my code so far. It's not displaying any errors but when I run the code it just load the form but the dgv does not appear. What can I do?
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As Date = Date.Now
Dim apocap As Date = Date.Parse(#8/22/2050#)
Dim loopdate As Date = start
While start < apocap
Dim dgv As New DataGridView
With dgv
.Size = New Size(250, 250)
.ColumnCount = 2
.RowCount = 12
.Location = New Point(12, 9)
.Visible = True
End With
start = start.Date.AddDays(1)
End While
End Sub
End Class
You have to add them to your form. Additionally, you'll want to change the .Left and/or .Top (.Location) properties so they don't all stack on top of each other:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As DateTime = DateTime.Today
Dim apocap As New DateTime(2050, 8, 22)
Dim i As Integer = 0
While start < apocap
Dim dgv As New DataGridView
With dgv
.Size = New Size(250, 250)
.ColumnCount = 2
.RowCount = 12
.Location = New Point(12, (9 + (250 * i)))
.Visible = True
End With
Me.Controls.Add(dgv)
i += 1
start = start.AddDays(1)
End While
End Sub
For fun, I like to write it like this:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim start As DateTime = DateTime.Today
Dim apocap As New DateTime(2050, 8, 22)
Dim count As Integer = CInt((apocap - start).TotalDays)
Me.Controls.AddRange(Enumerable.Range(0, count).Select(
Function(i)
Return New DataGridView With {
.Size = New Size(250, 250),
.ColumnCount = 21,
.RowCount = 12,
.Location = New Point(12, (9 + (250 * i))),
.Visible = True
}
'start.AddDays(i) is there if you need it
End Function
).ToArray())
End Sub

How to add picture boxes in rows in vb 2010

I am not sure how to create picture boxes on multiple rows in vb 2010. At the moment, I am only able to create them on
one row and one picture box on the second row. (Eventually I would like to add in 5 rows with 10 pictureboxes on each row) I have used the follow code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim xPosition As Integer = 20
Dim yPosition As Integer = 40
For i As Integer = 1 To 20
Dim pb As New PictureBox
With pb
If i < 20 Then
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(xPosition, yPosition)
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
xPosition += 70
ElseIf i > 10 Then
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(20, 120)
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
xPosition += 70
End If
Dim thisSeating As New Seating
With thisSeating
.SeatNumber = i
.PB = pb
.Occupied = False
End With
seatingList.Add(thisSeating)
End With
Next
End Sub
If anyone would be willing to help me or direct me to the correct path, I would be very grateful :)
Thanks in advance
Is this what you're looking for?
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim xPosition As Integer = 20
Dim yPosition As Integer = 40
For i As Integer = 1 To 5
For j As Integer = 1 To 10
Dim pb As New PictureBox
With pb
.Name = "PictureBox" & i.ToString
.SizeMode = PictureBoxSizeMode.Zoom
.Size = New Size(60, 60)
.Location = New Point(xPosition + (j * 70), yPosition + (i * 100))
.Image = My.Resources.Seating_No_Person
Me.Controls.Add(pb)
AddHandler pb.Click, AddressOf PictureBox_Click
Dim thisSeating As New Seating
With thisSeating
.SeatNumber = i
.PB = pb
.Occupied = False
End With
seatingList.Add(thisSeating)
End With
Next
Next
End Sub

Issue with combobox autocomplete

I am trying to incorporate three comboboxes into a functional GUI which consist of textboxes, numericupdown controls and now comboboxes.
On those form I have navigation with keyboard (me.selectnextcontrol) with keys up and down.
What happens?
When I step in those GUI for first time everything work OK and I can move up/down with keyboard as expected.
Problem becomes when I edit a combobox which is in the midle of my gui and is set like this:
mycombo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
mycombo.AutoCompleteSource = AutoCompleteSource.CustomSource
mycombo.AutoCompleteCustomSource = myAutoCompleteStringCollection
Problem is that when I come back to those combobox my navigation loop don't get keypress (up or down keys) anymore because combobox takes them for itselfs purpose (change index).
I try with mycombo.AutoCompleteSource = AutoCompleteSource.CustomSource after combobox_Leave but that turns off autocomplete what is not wanted.
Question is:
Is here possible to set described combobox after usage in mode like it was at the beginning of program, when it was not edited, so I can navigate with keyboard through such comboboxes at initial way but that autosuggest option remain enabled if I need to edit it again.
EDITED:
Here is simple example which shows a problem:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim tb As New TextBox
Dim cbb As New ComboBox
Dim tbb As New TextBox
Dim b1 As New Button
Dim b2 As New Button
With Me
.KeyPreview = True
.Size = New Size(350, 200)
With .Controls
.Add(tb)
With tb
.TabIndex = 0
.Location = New Point(95, 20)
.Text = "This is"
End With
.Add(cbb)
With cbb
.TabIndex = 1
.Items.AddRange(New String() {"alabama", "africa", "australia", "grenland"})
.Location = New Point(95, 50)
.Text = "an Example"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
.Add(tbb)
With tbb
.TabIndex = 2
.Location = New Point(95, 80)
.Text = "textbox"
End With
.Add(b1)
With b1
.TabStop = False
.Location = New Point(90, 130)
.Text = "Nothing"
End With
.Add(b2)
With b2
.TabStop = False
.Location = New Point(170, 130)
.Text = "Exit"
AddHandler b2.Click, AddressOf b2_Click
End With
End With
End With
End Sub
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If e.KeyCode = Keys.Up Then
e.Handled = True
Me.SelectNextControl(Me.ActiveControl, False, True, True, True)
End If
If e.KeyCode = Keys.Down Then
e.Handled = True
Me.SelectNextControl(Me.ActiveControl, True, True, True, True)
End If
End Sub
Private Sub b2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
End Class
When you start this program pass several times through all controls with key down arrow. Then stop on combobox and type letter "a", then try to navigate again with key down arrow.
Public Class Form1
Private tb As New TextBox
Private cbb As New ComboBox
Private tbb As New TextBox
Private b1 As New Button
Private b2 As New Button
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler cbb.PreviewKeyDown, AddressOf cbb_PreviewKeyDown
AddHandler tb.PreviewKeyDown, AddressOf tb_PreviewKeyDown
AddHandler tbb.PreviewKeyDown, AddressOf tbb_PreviewKeyDown
Me.Size = New Size(350, 200)
With tb
.Parent = Me
.TabIndex = 0
.Location = New Point(95, 20)
.Text = "This is"
End With
With cbb
.Parent = Me
.TabIndex = 1
.Items.AddRange(New String() {"alabama", "africa", "australia", "grenland"})
.Location = New Point(95, 50)
.Text = "an Example"
.DropDownStyle = ComboBoxStyle.DropDown
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.ListItems
End With
With tbb
.Parent = Me
.TabIndex = 2
.Location = New Point(95, 80)
.Text = "textbox"
End With
With b1
.Parent = Me
.TabStop = False
.Location = New Point(90, 130)
.Text = "Nothing"
End With
With b2
.Parent = Me
.TabStop = False
.Location = New Point(170, 130)
.Text = "Exit"
AddHandler b2.Click, AddressOf b2_Click
End With
End Sub
Private Sub tb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
If e.KeyCode = Keys.Up Then tbb.Focus()
If e.KeyCode = Keys.Down Then cbb.Focus()
End Sub
Private Sub cbb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
If e.KeyCode = Keys.Up Then
cbb.AutoCompleteMode = AutoCompleteMode.None
tb.Focus()
cbb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
If Not cbb.Items.Contains(cbb.Text) Then cbb.Text = ""
End If
If e.KeyCode = Keys.Down Then
cbb.AutoCompleteMode = AutoCompleteMode.None
tbb.Focus()
cbb.AutoCompleteMode = AutoCompleteMode.SuggestAppend
If Not cbb.Items.Contains(cbb.Text) Then cbb.Text = ""
End If
End Sub
Private Sub tbb_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs)
If e.KeyCode = Keys.Up Then cbb.Focus()
If e.KeyCode = Keys.Down Then tb.Focus()
End Sub
Private Sub b2_Click()
Me.Close()
End Sub
End Class
I modified your code. And now, it's working. :)

vb.net disable runtime created buttons

Hey all i have created dynamic buttons at runtime and i would like to disable them when a user clicks on a form button.
This is the code i have for that button:
Dim intXX As Integer = 0
Do Until intXX = intX
userAvatar(intXX).Enabled = False
intXX = intXX + 1
Loop
The buttonNames is an array of all populated button names created at runtime. However, trying the .enabled = false at the end of that does not work. What other ways are there to do that with buttons created at runtime?
How i create the buttons are like this:
private sub createButton()
Dim personAvatar As New PictureBox
With personAvatar
.AutoSize = False '> ^
If intX = 7 Then
thePrviousAvatarImg = 0
End If
If intX <= 6 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 10)
ElseIf intX >= 7 And intX <= 14 Then
.Location = New System.Drawing.Point(10 + thePrviousAvatarImg, 150)
Else
Exit Sub
End If
.Name = "cmd" & nameOfPerson
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BackgroundImage = Image.FromFile(theAvatarDir)
.Tag = nameOfPerson
.BringToFront()
End With
AddHandler personAvatar.Click, AddressOf personAvatar_Click
Me.Controls.Add(personAvatar)
userAvatar(intX) = personAvatar
intX = intX + 1
End With
End Sub
Thanks for your time and help!
David
You can't refer to button objects using a string representation of their names. Instead of using buttonNames (which I assume is an array of strings), use an array of buttons and add each button to that. Then loop through that array (as you've done here) setting enabled = false on each one.
So before you create each picture box, declare an array to store them:
Dim MyPictureBoxes() as PictureBox
Then as you create each one, add them to the array. When you're done creating them, you can disable them like this:
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.enabled = False
Next
I've created a form with two buttons, cmdGo and cmdDisable, to demonstrate this more completely:
Public Class Form1
Dim MyPictureBoxes(4) As PictureBox
Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
Dim personAvatar As New PictureBox
Dim intX As Integer = 0
While intX < 5
personAvatar = New PictureBox
With personAvatar
.AutoSize = False
.Left = intX * 100
.Top = 100
.Name = "cmd" & intX
.Size = New System.Drawing.Size(100, 100)
.TabStop = False
.Text = ""
.BorderStyle = BorderStyle.FixedSingle
.BackgroundImageLayout = ImageLayout.Center
.BackColor = Color.LightGray
.BringToFront()
End With
Me.Controls.Add(personAvatar)
MyPictureBoxes(intX) = personAvatar
intX = intX + 1
End While
End Sub
Private Sub cmdDisable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDisable.Click
For Each MyPictureBox In MyPictureBoxes
Debug.Print(MyPictureBox.Name)
MyPictureBox.Enabled = False
Next
End Sub
End Class
Notice how MyPictureBoxes is scoped for the whole form so it's accessible to both subs.