Click here to Skip to main content
15,891,850 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
Hi,

I'm someone who is reasonable at VBA and I'm just making the giant leap to VB.net. What I want my program to do is essentially

1)Time events using checkboxes
2)Display the total time of an event in a text box.
3)Output into excel

Imagine I have CheckBox1 and TextBox1.

How would I press checkbox1 which would start a timer in Textbox1 that would be visible as the timer was counting. The timer would then stop when the checkbox was un-ticked and that would be logged as an event. If I clicked the box again a new event would be started and so on. Each event would be logged and saved

I currently have no code, just a form. Any help would be appreciated and thank you for your time

Sean

What I have tried:

This is my first resource, I've built my form and added a timer from the components in VB.net 2017
Posted
Updated 11-Dec-17 14:38pm
Comments
Member 13569975 12-Dec-17 5:52am    
Hi it's me again. I've made a simple code for two timers and a total timer. I wanted the total timer to be synchronised with the check boxes I had so I did a really basic IF statement. Can I just ask out of interest how bad my code is and how I'd make it better? I don't want to get in bad habits right from the start

Public Class Form1
Dim ht As Integer
Dim mt As Integer
Dim st As Integer

Dim h As Integer
Dim m As Integer
Dim s As Integer

Dim h1 As Integer
Dim m1 As Integer
Dim s1 As Integer
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.CheckState = CheckState.Unchecked Then

Timer1.Stop()

End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
s += 1
CPT.Text = h & ":" & m & ":" & s


CPT.Text = h & ":" & m & ":" & s
If s = 60 Then
s = 0
m += 1
CPT.Text = h & ":" & m & ":" & s
If m = 60 Then
m = 0
h += 1
CPT.Text = h & ":" & m & ":" & s
End If

End If

End Sub

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
s1 += 1
Sampler.Text = h1 & ":" & m1 & ":" & s1


If s1 = 60 Then
s1 = 0
m1 += 1
CPT.Text = h1 & ":" & m1 & ":" & s1
If m1 = 60 Then
m1 = 0
h1 += 1
CPT.Text = h1 & ":" & m1 & ":" & s1

End If

End If


End Sub

Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
If CheckBox2.CheckState = CheckState.Unchecked Then


Timer2.Stop()

End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckBox1.CheckState = CheckState.Unchecked And CheckBox2.CheckState = CheckState.Unchecked Then

MsgBox("Select Operational Type First")

Else

Timer18.Start()
If CheckBox1.CheckState = CheckState.Checked Then

Timer1.Start()
End If

If CheckBox2.CheckState = CheckState.Checked Then

Timer2.Start()
End If

End If
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Timer18.Stop()
Timer1.Stop()
Timer2.Stop()
End Sub

Private Sub Timer18_Tick(sender As Object, e As EventArgs) Handles Timer18.Tick
st += 1
Total.Text = ht & ":" & mt & ":" & st


Total.Text = ht & ":" & mt & ":" & st
If st = 60 Then
st = 0
mt += 1
Total.Text = ht & ":" & mt & ":" & st
If mt = 60 Then
mt = 0
ht += 1
Total.Text = ht & ":" & mt & ":" & st
End If

End If
End Sub
End Class

1 solution

Do it step by step: first look how the timer works.

Forget about writing to excel, that is another task that is not depending on timer's task for now.

So you would now initialize your timer, start it by click-on the check box and stop it by click-off the check box and write the current time on the label.

First after you are ready with this task, take a look how to communicate with excel:
How to create Excel file in VB.Net
 
Share this answer
 
v2
Comments
Member 13569975 11-Dec-17 12:10pm    
Thank you for the response. I've changed things slightly. I've used labels as timer counters as it's much neater and easier. My code is

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
ml += 1
CPT.Text = m & ":" & s & ":" & ml

If ml = 100 Then
ml = 0
s += 1
CPT.Text = m & ":" & s & ":" & ml
If s = 60 Then
s = 0
m += 1
CPT.Text = m & ":" & s & ":" & ml
If m = 60 Then
m = 0
h += 1
CPT.Text = m & ":" & s & ":" & ml
End If

End If
End If
End Sub

I'll work my way through your links. Thanks for the help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900