Welcome to Dream.In.Code
Become a VB.NET Expert!

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!




Looping and timers

2 Pages V  1 2 >  
Reply to this topicStart new topic

Looping and timers

RudyVB.net
29 May, 2008 - 06:36 AM
Post #1

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hi all!

Seem simple enough. Here is the code.

CODE
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.

So how can I work around this?

Thanks!

Rudy
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Looping And Timers
29 May, 2008 - 08:49 AM
Post #2

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
Hey Rudy,

What are you trying to do?

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?
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Looping And Timers
29 May, 2008 - 09:10 AM
Post #3

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

QUOTE(djkitt @ 29 May, 2008 - 09:49 AM) *

Hey Rudy,

What are you trying to do?

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.

Do you have any suggestions? I'm all ears!

Thanks! smile.gif

Rudy
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Looping And Timers
29 May, 2008 - 10:20 AM
Post #4

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
OK Rudy,

I think I have an idea for you...

When you create the label and the button use a number to give them unique names based on that number:

CODE

Dim btn As New Button
btn.Name = "btn" + uniqueId.ToString
btn.Tag = uniqueId.ToString
Controls.Add(btn)
uniqueId++;


CODE

'inside button click event...
btn = sender
Dim lbl As New Label
lbl.Name = "lbl" + btn.Tag
Controls.Add(lbl)
tmr.Tag = btn.Tag


Then inside the TimerTick event use the string in tmr.tag to grab the label and button for messing with.

CODE

'inside timer tick event
tmr = sender
lbl = Me.Controls("lbl" + tmr.Tag)
btn = Me.Controls("btn" + tmr.Tag)


I would think that should work.

Hope that helps.

This post has been edited by djkitt: 29 May, 2008 - 11:28 AM
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Looping And Timers
29 May, 2008 - 11:31 AM
Post #5

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hi Kitt!

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)
                
                frmAT.txtCommand.Clear()
                frmAT.txtCommand.Text = String.Concat(prtFrmInm.ToString.Remove(1, 2), "00".ToString)

                frmAT.btnTest.PerformClick()

            End If

        End If

    End Sub


Thanks for all you help!

Rudy
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Looping And Timers
29 May, 2008 - 11:56 AM
Post #6

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
Hey Rudy,

I actually wrote a little test prog and it does work from the tick event. Nothing fancy, just to test the concept.

I have a Form1 with a Button1 on it. If anything looks goofy it is probably because I normally code in C#.

When You click the button it creates a dynamic button. If you click on the dynamic button a label is created and a timer is started.

CODE

Public Class Form1

    Dim btnNum 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" + btnNum.ToString
        btn.Top = 60
        btn.Left = btnNum * 80
        btn.BackColor = Color.AntiqueWhite
        btnNum += 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 tmr As Timer
        Dim btn As Button
        Dim lbl As Label
        Dim count As Integer

        tmr = sender
        tmr.Enabled = False
        lbl = Me.Controls("lbl" + tmr.Tag)

        count = lbl.Tag
        count -= 1

        lbl.Tag = count
        lbl.Text = count.ToString

        If (count = 0) Then
            btn = Me.Controls(tmr.Tag.ToString)
            btn.BackColor = Color.AntiqueWhite

            Me.Controls.Remove(lbl)
            tmr.Dispose()
            lbl.Dispose()
            btn.Enabled = True

        Else
            tmr.Enabled = True
        End If

    End Sub




    Private Sub DynBtn_Click2(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim btn As Button
        btn = sender
        btn.Enabled = False

        btn.BackColor = Color.Blue

        Dim lbl As New Label
        lbl.Name = "lbl" + btn.Name
        lbl.Width = 60

        lbl.Tag = 30
        lbl.Top = 30
        lbl.Left = btn.Left
        lbl.Text = "30"

        Me.Controls.Add(lbl)

        Dim tmr As New Timer
        tmr.Tag = btn.Name
        tmr.Interval = 100
        AddHandler tmr.Tick, AddressOf OnTimerEvent
        tmr.Start()

    End Sub

End Class


(If you feel like wasting time make ten buttons and try to keep them all blue. Hoo-boy! What fun.)

This post has been edited by djkitt: 29 May, 2008 - 12:22 PM
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Looping And Timers
29 May, 2008 - 12:40 PM
Post #7

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hi Kitt!

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.

Thanks again for your help on this.

Rudy


User is offlineProfile CardPM
+Quote Post

djkitt
RE: Looping And Timers
29 May, 2008 - 01:07 PM
Post #8

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
QUOTE(RudyVB.net @ 29 May, 2008 - 03:40 PM) *


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?


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'.



User is offlineProfile CardPM
+Quote Post

djkitt
RE: Looping And Timers
29 May, 2008 - 01:18 PM
Post #9

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
I changed the code a bit here.

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

        timerTimer = sender
        timerTimer.Enabled = False
        timerLabel = Me.Controls("lbl" + timerTimer.Tag)

        count = timerLabel.Tag
        count -= 1

        timerLabel.Tag = count
        timerLabel.Text = count.ToString

        If (count = 0) Then
            timerButton = Me.Controls("btn" + timerTimer.Tag)
            timerButton.BackColor = Color.AntiqueWhite

            Me.Controls.Remove(timerLabel)
            timerTimer.Dispose()
            timerLabel.Dispose()
            timerButton.Enabled = True

        Else
            timerTimer.Enabled = True
        End If

    End Sub




    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

        lbl.Width = 60

        lbl.Tag = 30
        lbl.Top = 30
        lbl.Left = dynamicBtn.Left
        lbl.Text = "30"

        Me.Controls.Add(lbl)

        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
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Looping And Timers
29 May, 2008 - 01:22 PM
Post #10

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

[quote]
[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.
[/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 blink.gif
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.

Rudy
User is offlineProfile CardPM
+Quote Post

djkitt
RE: Looping And Timers
29 May, 2008 - 01:36 PM
Post #11

D.I.C Head
**

Joined: 22 May, 2008
Posts: 134



Thanked: 14 times
My Contributions
Hey Rudy,

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
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Looping And Timers
29 May, 2008 - 01:49 PM
Post #12

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Thanks Kitt!

I think that does makes sense. I'll take a look through the code. At least I know where I'm looking, the unique Id is key.

I'll let you know how it goes.

Thanks for sticking with me on this!

Rudy
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:15PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month