Hello all!
I have a button that is created dynamicly, and based on that button click, a timer and a label with that button. So every button click, there is more than one button created. A timer and label is created for that button.
So when the label = 0, the timer stops. I also want to change the image of the button, when timer stops as well. The thing that is tricky,or at least for me. Each button has it own timer and stuff, so when the image changes, it changes the right button.
I'll supply some code, hopefully this makes sense. I know this can be done, I'm just not sure how to do it.
So this is where I'm creating button based on a boolean value on a sql DB.
CODE
Buttons = GetButtonData()
For Each bi As ButtonInfo In Buttons
If bi.Enabled Then
'Dim prtLabel As New Label
' MsgBox(prtLabel.Text.ToString)
Dim newbutton As New Button
newbutton.Text = bi.VisText
newbutton.TextAlign = ContentAlignment.TopCenter
newbutton.Tag = bi
newbutton.Width = 102
newbutton.Height = 82
newbutton.Top = 60 '+ (0 + bi.VisId)
newbutton.Left = -95 + (100 * bi.VisId)
'MsgBox(newbutton.Location.ToString)
newbutton.Image = My.Resources.pvmetal2
If newbutton.Left >= 906 Then
newbutton.Top = 175 '+ '(0 + bi.VisId)
newbutton.Left = -1095 + (100 * bi.VisId)
' MsgBox(newbutton.Location.ToString)
End If
If newbutton.Left >= 906 Then
newbutton.Top = 290 '+' (0 + bi.VisId)
newbutton.Left = -2095 + (100 * bi.VisId)
End If
If newbutton.Left >= 906 Then
newbutton.Top = 405 '+ '(0 + bi.VisId)
newbutton.Left = -3095 + (100 * bi.VisId)
End If
If newbutton.Left >= 906 Then
newbutton.Top = 420 '+ (0 + bi.VisId)
newbutton.Left = -4095 + (100 * bi.VisId)
End If
newbutton.Name = bi.VisId.ToString
AddHandler newbutton.Click, AddressOf Buttons_Click
Me.Controls.Add(newbutton)
End If
Next
This is the button click event for the button NEW BUTTON.
CODE
Dim btn As Button = sender
If btn.Tag IsNot Nothing AndAlso TypeOf (btn.Tag) Is ButtonInfo Then
'btn.Image = My.Resources.pvmetal2red
Application.DoEvents()
Try
Dim frm As New frmInmates(btn.Tag)
'This sends the name to the connect form
frm.lblHoldVis.Text = btn.Text
'frmAT.txtCommand.Text = btn.Tag.ToString
MsgBox(btn.Name.ToString)
frm.ShowDialog()
Finally
'THIS WHERE MY TIMER AND MY LABEL FOR THAT TIMER GETS CREATED
btn.Image = My.Resources.pvmetal2red
Dim lbl As New Label
lbl.Name = "lblTimer"
lbl.Text = "30"
lbl.Tag = lbl.Text.ToString
Dim MinFont As New Font("arial", 12, FontStyle.Bold)
lbl.Font = MinFont
lbl.TextAlign = ContentAlignment.MiddleCenter
lbl.AutoSize = True
Me.Controls.Add(lbl)
Dim tmr As New Timer
tmr.Interval = 1000
AddHandler tmr.Tick, AddressOf timers_tick
tmr.Tag = lbl
tmr.Start()
Dim C As Control = CType(sender, Control)
lbl.Location = New Point(C.Left + 30, C.Bottom)
Dim lblInmV As New Label
lblInmV.Name = "lblInmVF"
lblInmV.Text = InmtrxVis
lblInmV.Font = MinFont
lblInmV.TextAlign = ContentAlignment.MiddleCenter
lblInmV.AutoSize = True
Me.Controls.Add(lblInmV)
'lblInmV.Location = New Point(C.Left + 30, 45)
lblInmV.Location = New Point(C.Left + 20, C.Top - 15)
End Try
End If
This is the tick event for the dymaiclu created timer
CODE
Dim lbl As Label = CType(sender, Timer).Tag
Dim i As Int32 = CInt(lbl.Tag)
i -= 1
lbl.Tag = i
'If lbl.Text < 10 Then
' lbl.Text = "0" & i.ToString
'Else
lbl.Text = i.ToString
'End If
If i = 0 Then
CType(sender, Timer).Stop()
If lbl.Text = "0" Then
THIS IS WHERE I NEED TO ACCESS "'btn.Image = My.Resources.pvmetal2red" But I'm not sure how to access btn, because it's only in the above sub. I can't make it public, or it will only make 1 button, and not the many that needed to be created.
End If
End If
Thanks for your help on this!
Rudy