Harry Allen Harry Allen - 1 year ago 45
Vb.net Question

Changing control (that is in an array) from a timer: Not working

I'm trying to make a picture's visibility toggle on a timer. The picture I am trying to change is underscore(i). Here is that code:

Dim DigitSelected As Integer = 1
Public Underscores(3) As PictureBox
Private Sub CursorTimer_Tick(sender As System.Object, e As System.EventArgs) Handles CursorTimer.Tick
Me.Underscores(DigitSelected).Visible = Not (Me.Underscores(DigitSelected).Visible)
End Sub

This code above was previously working, but recently I moved where I was creating the pictureboxes and their stuff to a module on another page:

With Initials
For i As Byte = 1 To 3
.Underscores(i) = New PictureBox
With .Underscores(i)
.Height = 60
.Width = 144
.ImageLocation = "Underscore.png"
.BackColor = Color.Transparent
End With
end with

Now when I use the top-most snippet of code, it throws no errors, and changes nothing. I'm pretty sure I'm just missing something small.

Any ideas? Thanks!

Answer Source

I don't exactly know what you are trying to achieve here, but here is what happens :

Everytime you run that loop, you create three transparent PictureBox that you add to your Main Form. You don't remove anything, nor hide anything !

If you want to toggle visibility, that is no way of doing so !

The best would be for you to know the PictureBoxes names, so you can do :

Dim pb1 = CType(Initials.FindControl("MyPictureBox", true), PictureBox)
If Not IsNothing(pb1) Then
    pb1.Visible = Not pb1.Visible
End If