Join 150,077 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,756 people online right now. Registration is fast and FREE... Join Now!
Do Until tmr.Enabled = False btn.Image = My.Resources.pvmetal2red Loop
The problem I have with this is that my timer starts in this block. It is a button click event. The tmr start before the loop. But when the loop starts, guess what? it loops. The the time_tick event doesn't run, it's waiting until I get to that block.
Now if I put a msgbox under my Do Until line, when the msgbox kick off, and I hit OK, it will then go to the tick event. Then after that tick event, it will goe back to the loop. And eventually the timer will run out, and the tmr will be disabled.
It looks like you are looping and continously setting the button image to the same image.
Depending on what you are doing I think you would probably be better served by starting your timer, setting your image once, and then changing the button image back when you are done with whatever the timer is doing.
Are you trying to block the user from clicking the button again?
It looks like you are looping and continously setting the button image to the same image.
Depending on what you are doing I think you would probably be better served by starting your timer, setting your image once, and then changing the button image back when you are done with whatever the timer is doing.
Are you trying to block the user from clicking the button again?
Hi Kitt!
I'm working the same problem I had before, just a diffrent approach. The original problem is change the background image when my timer stops. But, because I have dynamic buttons created, and on the button click of those dydnamicly created buttons, I have timer dyanmicly built, and labels dynamicly buitl as well. Based on a boolean valuse in a DB, will determine if a button is created or not.
It would be nice to say when the timer.tag = 0, btn.image + my.resoureces.mynewimage. But that problem is, I can not get to the btn properties from the timer_click sub. If I Public btn at new button, it will only create the one button. So I have to Dim the button. But, with that I can't get to it, beside within the button click. In the button click is the only place where I know what button was clicked, and what timer is running.
So I thought, I could create a loop or something in the button_click event, check to see if the timer is running. Or I can monitor the timer.tag, or the label for the timer, and whe that hits 0, change my image. I originally didn't have it so it changes the images every time, just trying stuff.
I would make a flag or something, and based on that flag, then change the value. Now when I put a msgbox in the loop, it will then continue with the timer. How can I have the timer run, and this loop run, at the same time.
I have come to the conclusion that I'm going to have to work within the button click event.
Thanks for the info. I tried that and it didn't work. I am using the tag number of the buttons to identify the button. Bottom line, if I could get acces to the btn property from the tick event, I would be set. I just don't know how, or if I even could do that. If you want I can show the code I'm working with. This is the button click event
CODE
Public Sub Buttons_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btn As Button = sender
If btn.Tag IsNot Nothing AndAlso TypeOf (btn.Tag) Is ButtonInfo Then
'btn.Image = My.Resources.pvmetal2red
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
frm.ShowDialog()
Finally ''btn.Image = My.Resources.pvmetal2red Dim lbl As New Label lbl.Name = "lblTimer" lbl.Text = "5"
lbl.Tag = lbl.Text.ToString Dim MinFont As New Font("arial", 12, FontStyle.Bold) lbl.Font = MinFont lbl.TextAlign = ContentAlignment.MiddleCenter lbl.AutoSize = True lbl.BackColor = Color.Transparent
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 + 20, C.Top - 15)
End Try
End If
End Sub
Here is the tick event
CODE
Private Sub timers_tick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lbl As Label = CType(sender, Timer).Tag Dim i As Int32 = CInt(lbl.Tag)
i -= 1
lbl.Tag = i
lbl.Text = i.ToString
If i = 0 Then CType(sender, Timer).Stop()
If lbl.Text = "0" Then I WOULD LIKE TO PUT btn.image = my.resource.myimage HERE BUT btn IS NOT PART OF THIS SUB, AND IT'S PRIVATE. AND btn REPRESENT THE BUTTON CLICK OF THAT BUTTON. SO I KNOW WHAT IMAGE TO CHANGE. frmAT.txtCommand.Clear() frmAT.txtCommand.Text = String.Concat(prtFrmInm.ToString.Remove(3), "00".ToString)
I tried your code, and it worked. I didn't have any doubt your code didn't work, my lack of understanding.
So I noticed on my click event I didn't have
CODE
Dim tmr As Timer Dim btn As Button
Now I know when I create the timer and button, I write Dim tmr as NEW timer. If I dim this agains, is that going after the same object, because it's the same name?
CODE
btn = Me.Controls(tmr.Tag.ToString)
Can you explain how this is working. I think it taking the tag number from the timer. And that's where I'm storing my int for the countdown.
CODE
btn.Image = My.Resources.col_btngreen
But when I use this, I get the following error. Am I really using the same btn as I was in my clcik event? Object reference not set to an instance of an object.
Now I know when I create the timer and button, I write Dim tmr as NEW timer. If I dim this agains, is that going after the same object, because it's the same name?
When creating a button, label or timer for the first time you need to use 'NEW'. But you don't need to create a new object when you are trying to reference an existing button, label or timer. You are just creating a variable that will hold the already created object. If you use 'NEW' in this case you will actually create an object for the variable to point to and then make the variable point to a different object later.
QUOTE
CODE
btn = Me.Controls(tmr.Tag.ToString)
Can you explain how this is working. I think it taking the tag number from the timer. And that's where I'm storing my int for the countdown.
Oh, yeah. I can see how this would be confusing. When I quick wrote this program I didn't do it exactly like I said to in my earlier post. I named the btn "btn1" or whatever and then when I create the label it actually gets "lblbtn1" for a name. The string "btn1" gets stored in the Timer's Tag and the decrementing number gets stored in the Label's Tag property. Then in the tick event I just look up the dynamic Button using its name Me.Control(tmr.Tag) and the label by adding "lbl" to the front.
QUOTE
CODE
btn.Image = My.Resources.col_btngreen
But when I use this, I get the following error. Am I really using the same btn as I was in my clcik event? Object reference not set to an instance of an object.
What's happening here is that you are trying to use a variable that is not pointing to an object. When you *create* a button you need to use 'NEW'.
1. I am now using uniqueId and holding just the number rather than the actual button's name in btn.Tag and tmr.Tag 2. Added some comments to help you see where we need NEW and where we don't 3. Changed the variable names so they aren't always tmr, btn and label. Hopefully this will make things more clear.
CODE
Public Class Form1
Dim uniqueId As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim btn As New Button btn.Name = "btn" + uniqueId.ToString btn.Tag = uniqueId.ToString btn.Top = 60 btn.Left = uniqueId * 80 btn.BackColor = Color.AntiqueWhite uniqueId += 1
AddHandler btn.Click, AddressOf DynBtn_Click2
Controls.Add(btn) End Sub
Private Sub OnTimerEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim timerTimer As Timer 'No need for the NEW declaration here as we are not making a new instance Dim timerButton As Button 'No need for the NEW declaration here as we are not making a new instance Dim timerLabel As Label 'No need for the NEW declaration here as we are not making a new instance Dim count As Integer
Private Sub DynBtn_Click2(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim dynamicBtn As Button 'No need for NEW declaration here as we are not creating a new instance of a button but rather pointing this variable at an existing Button. [sender] dynamicBtn = sender dynamicBtn.Enabled = False
dynamicBtn.BackColor = Color.Blue
Dim lbl As New Label 'This needs the NEW declaration as we *are* making a new instance of Label. lbl.Name = "lbl" + dynamicBtn.Tag
Dim tmr As New Timer 'This needs the NEW declaration as we *are* making a new instance of Timer tmr.Tag = dynamicBtn.Tag tmr.Interval = 100 AddHandler tmr.Tick, AddressOf OnTimerEvent tmr.Start()
End Sub
End Class
This post has been edited by djkitt: 29 May, 2008 - 01:19 PM
But when I use this, I get the following error. Am I really using the same btn as I was in my clcik event? Object reference not set to an instance of an object. [/quote]
What's happening here is that you are trying to use a variable that is not pointing to an object. When you *create* a button you need to use 'NEW'. [/quote][/quote]
Ok. But at this point, I am not creating a new button, I'm just trying to get to an click event of a button that already had been created. This from the timer_tick event. This is very confusing The reason I don't really need to get to the button, because it won't tell me what button had been clciked. If I go to the click event, than I will know what button was clicked. So maybe I'm asking the wrong question. I know I can access other objects, weather in diffrent forms or whatever. I guess I should be asking, how to access a click_event? The click event should give me all of that buttons info, including the background image.
Sorry to keep hammering on this. If I already the buttons created, this would be easy. But creating everything on the fly, it's totally diffrent.
I put up code with some comments above. Take a look through it and see if it isn't a little more clear.
The way I am getting the button from the timer_tick event is to pass the button's uniqueId up through the Timer's Tag property and then using that to grab the correct button from the form's Controls. This uniqueId was used to create both the button and the label so you can access both from the tick event now.
The difference from your code is that you are passing a pointer to the actual instance of a label using the Timer's Tag property while I am just passing a number that lets me recreate the label and the button names.
"lbl"+uiniqueId "btn"+uniqueId
Does that make sense?
This post has been edited by djkitt: 29 May, 2008 - 01:37 PM