# VB 2010 Express Counters



## ICEstes (Feb 24, 2011)

Good morning TSF!

Allow me to quickly introduce myself before I beg for help. I am a 2nd Class IT in the United States Coast Guard. I work with computer hardware, Microsoft Exchange, phone switches, network gear and a host of other stuff. Unfortunately I am not a programmer like my command would like to believe.

So he comes the begging. 

THE SETUP
I am attempting to write a simple program in VB 210 Express (it is free!) that contains a checklist and Timers. The Checklist part itself seems easy enough. It is the timers that are giving me a hard time. This program has a total of 17 timers. All linked to seperate buttons all keeping track of the amount of time an indivdual is on air during Firefighting Casualties.

THE PROBLEM
When I run the program, all of my timers appear to work, but then when I reset one of they will all reset. This is a major problem, If I reset one timer because a person is no longer on Air and it resets the people who are still on air....well you see where this is going. Bottom line is I need to know exactly how long individuals have been on air.

The CODE

```
Public Class Form1
    Dim time As New DateTime

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If timer1.enabled = True Then
            timer1.stop()
        Else
            time = DateTime.Now
            timer1.start()

        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time)
        Label1.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Timer2.Enabled = True Then
            Timer2.Stop()
        Else

            time = DateTime.Now
            Timer2.Start()
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time)
        Label2.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Timer3.Enabled = True Then
            Timer3.Stop()
        Else

            time = DateTime.Now
            Timer3.Start()
        End If
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time)
        Label3.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If Timer4.Enabled = True Then
            Timer4.Stop()
        Else

            time = DateTime.Now
            Timer4.Start()
        End If
    End Sub

    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time)
        Label4.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If Timer5.Enabled = True Then
            Timer5.Stop()
        Else

            time = DateTime.Now
            Timer5.Start()
        End If
    End Sub

    Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time)
        Label5.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub
End Class
```
THE SOLUTION?
I don't know. I am sure it is something I am simply not seeing. Not being a programmer and all. Any assistance at all is greatly appreciated.


----------



## niemiro (Feb 20, 2010)

Hello, and welcome to the Tech Support Forum!

Yes, it is reasonably simple, but not necessarily obvious. 

In that code sample, five timers, but only one time variable.

This code solves the problem in question:


```
Public Class Form1
    Dim time1 As New DateTime
    Dim time2 As New DateTime
    Dim time3 As New DateTime
    Dim time4 As New DateTime
    Dim time5 As New DateTime

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If timer1.enabled = True Then
            timer1.stop()
        Else
            time1 = DateTime.Now
            timer1.start()

        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time1)
        Label1.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If Timer2.Enabled = True Then
            Timer2.Stop()
        Else

            time2 = DateTime.Now
            Timer2.Start()
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time2)
        Label2.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub


    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If Timer3.Enabled = True Then
            Timer3.Stop()
        Else

            time3 = DateTime.Now
            Timer3.Start()
        End If
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time3)
        Label3.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If Timer4.Enabled = True Then
            Timer4.Stop()
        Else

            time4 = DateTime.Now
            Timer4.Start()
        End If
    End Sub

    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time4)
        Label4.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If Timer5.Enabled = True Then
            Timer5.Stop()
        Else

            time5 = DateTime.Now
            Timer5.Start()
        End If
    End Sub

    Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
        Dim difference As TimeSpan = DateTime.Now.Subtract(time5)
        Label5.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub
End Class
```
It isn't great, and if you want help making it better, in any certain way, simply ask, but it solves the problem keeping to the same coding style.

You did a great job  Well done.

Richard


----------



## ICEstes (Feb 24, 2011)

Thanks, I just implemented your code changes and it works flawlessly now. Again I appreciate the assistance. I am eager to learn more. So I am going to continue to code other small projects. Currently all that I have left in this project is a Time stamp that will let me know exactly when the application was launched.


----------



## niemiro (Feb 20, 2010)

Hello!

Really glad to have been of assistance! Keep up the programming if you can. You are doing really well, and obviously have a spark for it. If I can do anything more to help, don't hesitate to ask!

Richard


----------



## ICEstes (Feb 24, 2011)

Okay, editing my post because I was being an idiot. What I am trying to do is select a checkbox and have it log the current time. I don't need it to count, just not the current system time and apply it to a text box or label. Can someone point me in the right direction?

Initial Post, Ignore Below:
I'm probably doing something wrong here again. Not so much in my programming but my understanding of VB 2010 Express as an tool. In the same program I am trying to ad a couple check boxes. I believe I have my code right, however the checkbox does not sit inside Public Class Form 1.

For each Item I have coded, I have been double clicking the item in the visual part of the tool. This would switch me to the coding window with my item already sitting in the Public Class Form 1 field. Are Checkboxes different?


----------



## ICEstes (Feb 24, 2011)

I guess you cannot edit your reply more than once? Or maybe there is a time frame on the edit feature. Anyways, wanted to show the code I currently have for the Checkbox. It is very similar to the timers. I want it to display the current system time and not start from zero ever. This code below will show me the current time if I click on the label, as soon as I click the checkbox though it starts counting from zero.


```
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

        If CheckBox1.Checked = True Then
            Timer19.Stop()
        Else
            time19 = DateTime.Now
            Timer19.Start()
        End If
    End Sub



    Private Sub Label20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label20.Click
        Dim difference As TimeSpan = DateTime.Now.Subtract(time19)
        Label20.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub
End Class
```


----------



## Ninjaboi (Dec 27, 2010)

There is a 15 minute time frame from when you may edit your posts.


----------



## niemiro (Feb 20, 2010)

Hello!

I am actually a bit confused about what you want to achieve this time. Please bear with me!

All of this x 19:

1 timer
1 checkbox
2 labels
1 button

Clicking the button starts and stops the timer. The timer clicks away when it should. The time is clocking up on one label. When the button is pressed again, the time shown and the timer freezes.

When the checkbox is checked, the other label should also the start time. Does this require retrospective calculation of the start time, or are we assuming that the checkbox will always be checked (why have it at all then!)

At the moment, my very dodgy understanding would give me this. Would a screenshot help?

Thanks!

Richard


----------



## ICEstes (Feb 24, 2011)

Will do my best to explain, and show you what I have worked out in full so far. I have only given you bits of the picture as I didn't think you would need to see absolutly everything I am trying to accomplish. Just the individual aspects. All of the timers are working wonderfully thanks to your help from yesterday.

Okay, A quick screen capture of visual program might help.









To sidestep the checkboxes I did the following


```
Private Sub Label21_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label21.Click
        Dim difference As TimeSpan = DateTime.Now.Subtract(time21)
        Label21.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
    End Sub
```
To be honest, I may try to talk my command into leaving it in its current shape and forget about the checkboxes. If I have the time that is all I need, and the checkboxes just take up more room and are smaller spot to click on.


----------



## niemiro (Feb 20, 2010)

Hello!

I think I understand now...just one last tiny point. Which label is Label21? Then I will probably be able to help you!

Thanks!

Richard


----------



## ICEstes (Feb 24, 2011)

Label 21 is the one next to DC Central.

Thanks again for the Support. I don't know how you guys do this for a living. I can only imagine much more complicated code. Would make a fun past time though. Programming something just cause I want to sounds more interesting than programming what someone else wants me to.


----------



## niemiro (Feb 20, 2010)

Hello!

Sorry for the delay! I had to think about this program a bit! I think I have got it now. Will this do the trick?:


```
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

        If CheckBox1.Checked = True Then
                    Dim difference As TimeSpan = DateTime.Now.Subtract(time21)
        Label21.Text = difference.Hours.ToString & ":" & difference.Minutes.ToString & ":" & difference.Seconds.ToString
        Else
Label21.Text = "0:0:0"
        End If
    End Sub
```
I hope I have got it right now. I apologise for the delay. It isn't the easiest program for me to get my head around! lol.



> I don't know how you guys do this for a living. I can only imagine much more complicated code. Would make a fun past time though. Programming something just cause I want to sounds more interesting than programming what someone else wants me to.


I don't do it for a living at the moment. I am too young to work (have only just started senior school! - EDIT: Started coding C# properly age 11, am now 15 and a half.) The code does get harder, and better means become available (You probably suspected this, but all of your copy and pasting of code (code duplication) is not actually required. Programming languages such as VB.Net have a lot of stuff built into them to avoid code duplication in almost all situations, however, most of it isn't easy code to wield. Most of it is the almost feared Object Orientated Programming (OOP). People think that this is really, really, really hard. Hard for you yes , but very learnable. Just some people think that they can't. But to start with, you must learn the simple methods, with code duplication. You probably suspected that there must be another way all along. It may take a couple of years for you to get up to OOP though. Don't think that boring copy and paste and variable rename is all that there is too it )

Sadly though, much code for clients is very, very long, tedious, and boring, sitting for hours on end in a small office. 

Anyway, clean code will come in time!

Richard


----------



## ICEstes (Feb 24, 2011)

Not sure what I did wrong, :upset: but of the 4 checklist i created using visual basic, they are now corrupted and no longer work. Which means I am at square one again. Getting it back into the shape it was in won't be hard, just tedious work. I have learned quite a bit.

Since I am starting from scratch, how would you recommend doing the timers that I initially talked about. If I can have one timer and then subtract differences from it and the time I activate it specific button, well that seems more efficient to me than having 19 timers.


----------



## niemiro (Feb 20, 2010)

Hello!

Oh. How did it become corrupt? Is it fixable with a bit of assistance? This tool works miracles: ShadowExplorer.com - About

Give it a quick try. It has saved me a few times! lol. 

Otherwise, what you have at the moment isn't too bad. Making the code neat would actually require more code and effort than it is worth, I think. However, I suppose that you could set up a 1000 milisecond tick. You could then persist which buttons have been pressed, and then use the event to increment, incrementing manually rather than automatically. Not sure that it is really worth it though, and it might actually slow things down.

However, I can suggest one change, which you may or may not like. I don't like the way your buttons work. It isn't really intuitive or "nice feeling". This (untested, and written in a rush) code should give them a nice feeling:


```
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
	If timer1.enabled = True Then 
		timer1.stop() 
	Else If Label1.Text = "0:0:0" Then
		time = DateTime.Now 
		timer1.start() 
	Else 
		Label1.Text = "0:0:0"
	End If 
End Sub
```
Richard


----------



## ICEstes (Feb 24, 2011)

Richard,
I apologize for not getting back to you sooner. I have been traveling alot lately for a bunch of training. None of which will help me with this project. I did get to make it back up to NYC though. Haven't been there in 14 years. 

Anyways, I have been plugging away at recoding. Timers are working as before, I used your suggestion for the Checkboxs which is working great. Your final suggestion on the Buttons to start and stop the timers works, but I don't think I like the way it works. It added an extra click to the operation. In other words:

Click button = timer starting
Click Button again = timer reset
Click button third time = timer start again

The reason this doesn't work for me is because I need to know how long individual where on air so I can determine who I need to send back in to fight said fire/toxic gas.

Things I still need to do (according to EO).
Most important
1.Flashing Alarm for Air takes at 10 minutes
2.Print to Text file button

Way in the future (read next week according to my Engineering Officer)
1. Integration into MPCMS system (likely not going to happen)
2. Video feed from Helmet Cam of Fire Team (not sure if this is even doable in Visual Basic)

Good thing I can not be fired!


----------



## ICEstes (Feb 24, 2011)

Resureccting a dead post. I think I have most of my problems figured out. Thanks for all the help. 

I do have one last plea for help. Is there anyway to make a button that will create a function after being held for say 3 seconds?


----------



## Ninjaboi (Dec 27, 2010)

Don't worry, the threads not too old to be counted against you ( though next time you should create a new thread for your question ).

You can create a timer that would allow you to time how long the button is held down.

Understanding the timer control | Visual Basic 6 (VB6)

Once the button has been held for 3 seconds by the timer, you can create/execute a function that is of your choosing.


----------



## ICEstes (Feb 24, 2011)

Thanks again for the support. We have succesfully used this program several times during training excercises and it has been a huge highlight of my tour onboard CGC Mackinaw. 

Bravo Zulu to you all.


----------



## ICEstes (Feb 24, 2011)

Ninjaboi said:


> Don't worry, the threads not too old to be counted against you ( though next time you should create a new thread for your question ).
> 
> You can create a timer that would allow you to time how long the button is held down.
> 
> ...


Good to see that the link above utilized the same method for making my labels flash. Builds a bit of confidence. I'm not understanding though how to get my new timer to count how long a button is held. Can you offer any more guidance?


----------



## AlbertMC2 (Jul 15, 2010)

Hi

You do not need a timer.
You can try something like:


```
Public Class Form1
    Dim StartTime As DateTime

    Private Sub Button1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseDown
        StartTime = DateTime.Now
    End Sub

    Private Sub Button1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseUp
        Dim TimeDiff As TimeSpan
        TimeDiff = DateTime.Now.Subtract(StartTime)
        If TimeDiff.Seconds > 2 Then
            'Run some code
        End If
    End Sub
End Class
```


----------

