Little help how I could simplify it

NssB

Member
Joined
May 11, 2007
Messages
13
Programming Experience
Beginner
I have written a small single form windows application.
However, I feel for the purpose it serves, it is probably overly complicated.
I wanted to know if any of you could have a look and advise on how I could simplify it if at all possible. I have just moved over from VBScript/HTA's and haven't quite gotten into the OO way of thinking yet. But your opinions and suggestions are most welcome.

Essentially, what I am emulating is a telephony system at work. This app at the moment simply keeps an ongoing elapse of time for the individual aux codes. Please ask if further detail is required. I have not commented the code all that thoroughly yet, but should be clear enough to follow.



VB.NET:
Imports System.IO
Imports System.Diagnostics

Public Class Main
     'Stopwatch set for each Aux code to track
    Dim stopwatch1 As Stopwatch
    Dim stopwatch2 As Stopwatch
    Dim stopwatch3 As Stopwatch
    Dim stopwatch4 As Stopwatch
    Dim stopwatch5 As Stopwatch
    Dim stopwatch6 As Stopwatch
    Dim stopwatch7 As Stopwatch
    Dim stopwatch8 As Stopwatch
    Dim stopwatch9 As Stopwatch
    Dim stopwatch10 As Stopwatch



    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'LoadTime()
        stopwatch1 = New Stopwatch
        stopwatch2 = New Stopwatch
        stopwatch3 = New Stopwatch
        stopwatch4 = New Stopwatch
        stopwatch5 = New Stopwatch
        stopwatch6 = New Stopwatch
        stopwatch7 = New Stopwatch
        stopwatch8 = New Stopwatch
        stopwatch9 = New Stopwatch
        stopwatch10 = New Stopwatch
    End Sub

    Private Sub LoadTime()
        Dim oRead As System.IO.StreamReader
        oRead = File.OpenText("c:\auxtime.ini")
        Dim ctrl As Control
        While oRead.Peek <> -1
            For Each ctrl In SummaryText.Controls
                If (ctrl.GetType.ToString = "System.Windows.Forms.TextBox") Then
                    CType(ctrl, TextBox).Text = oRead.ReadLine()
                End If
            Next
        End While

        oRead.Close()
    End Sub

    Private Sub SaveTime()
        Dim oWrite As System.IO.StreamWriter
        oWrite = File.CreateText("c:\auxtime.ini")
        Dim ctrl As Control
        For Each ctrl In SummaryText.Controls
            oWrite.WriteLine(CType(ctrl, TextBox).Text)
        Next
        oWrite.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
        If stopwatch1.IsRunning Then
            ' Stop the timer; show the start and reset buttons.
            stopwatch1.Stop()
            Button1.Text = "1"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch1.Start()
            Button1.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If stopwatch2.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch2.Stop()
            Button2.Text = "2"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch2.Start()
            Button2.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If stopwatch3.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch3.Stop()
            Button3.Text = "3"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch3.Start()
            Button3.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        If stopwatch4.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch4.Stop()
            Button4.Text = "4"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch4.Start()
            Button4.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        If stopwatch5.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch5.Stop()
            Button5.Text = "5"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch5.Start()
            Button5.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        If stopwatch6.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch6.Stop()
            Button6.Text = "6"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch6.Start()
            Button6.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        If stopwatch7.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch7.Stop()
            Button7.Text = "7"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch7.Start()
            Button7.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        If stopwatch8.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch8.Stop()
            Button8.Text = "8"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch8.Start()
            Button8.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        If stopwatch9.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch9.Stop()
            Button9.Text = "9"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch9.Start()
            Button9.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click
        If stopwatch10.IsRunning Then

            ' Stop the timer; show the start and reset buttons.
            stopwatch10.Stop()
            Button0.Text = "0"
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            stopwatch10.Start()
            Button0.Text = "Available"
            DisableButtons()
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If stopwatch1.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch1.Elapsed

            ' Format and display the TimeSpan value.
            TextBox1.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        If stopwatch2.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch2.Elapsed

            ' Format and display the TimeSpan value.
            TextBox2.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        If stopwatch3.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch3.Elapsed

            ' Format and display the TimeSpan value.
            TextBox3.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        If stopwatch4.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch4.Elapsed

            ' Format and display the TimeSpan value.
            TextBox4.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
        If stopwatch5.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch5.Elapsed

            ' Format and display the TimeSpan value.
            TextBox5.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick
        If stopwatch6.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch6.Elapsed

            ' Format and display the TimeSpan value.
            TextBox6.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick
        If stopwatch7.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch7.Elapsed

            ' Format and display the TimeSpan value.
            TextBox7.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer8_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer8.Tick
        If stopwatch8.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch8.Elapsed

            ' Format and display the TimeSpan value.
            TextBox8.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer9_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer9.Tick
        If stopwatch9.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch9.Elapsed

            ' Format and display the TimeSpan value.
            TextBox9.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub

    Private Sub Timer10_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer10.Tick
        If stopwatch10.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = stopwatch10.Elapsed

            ' Format and display the TimeSpan value.
            TextBox0.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If
    End Sub



    Private Sub DisableButtons()
        Dim ctrl As Control
        For Each ctrl In Buttons.Controls
            If CType(ctrl, Button).Text <> "Available" Then
                CType(ctrl, Button).Enabled = False
            End If
        Next
    End Sub

    Private Sub EnableButtons()
        Dim ctrl As Control
        For Each ctrl In Buttons.Controls
            CType(ctrl, Button).Enabled = True
        Next
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles reset.Click
        Dim ctrl As Control
        For Each ctrl In SummaryText.Controls
            If (ctrl.GetType.ToString = "System.Windows.Forms.TextBox") Then
                CType(ctrl, TextBox).Text = "00:00:00"
            End If
        Next
        SaveTime()
    End Sub
End Class
 
Bear in mind I'm also a newbie, and I didn't spend much time with it, but I did clean it up a tad:

VB.NET:
Imports System.IO
Imports System.Diagnostics

Public Class Form1

    'Stopwatch set for each Aux code to track
    Dim stopwatch1 As Stopwatch
    Dim stopwatch2 As Stopwatch
    Dim stopwatch3 As Stopwatch
    Dim stopwatch4 As Stopwatch
    Dim stopwatch5 As Stopwatch
    Dim stopwatch6 As Stopwatch
    Dim stopwatch7 As Stopwatch
    Dim stopwatch8 As Stopwatch
    Dim stopwatch9 As Stopwatch
    Dim stopwatch10 As Stopwatch

    Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        LoadTime()
        stopwatch1 = New Stopwatch
        stopwatch2 = New Stopwatch
        stopwatch3 = New Stopwatch
        stopwatch4 = New Stopwatch
        stopwatch5 = New Stopwatch
        stopwatch6 = New Stopwatch
        stopwatch7 = New Stopwatch
        stopwatch8 = New Stopwatch
        stopwatch9 = New Stopwatch
        stopwatch10 = New Stopwatch

    End Sub

    Private Sub LoadTime()

        Dim oRead As System.IO.StreamReader
        oRead = File.OpenText("c:\auxtime.ini")
        Dim ctrl As Control
        While oRead.Peek <> -1

            For Each ctrl In Me.Controls
                If (TypeOf ctrl Is TextBox) Then
                    ctrl.Text = oRead.ReadLine()
                End If
            Next

        End While

        oRead.Close()

    End Sub

    Private Sub SaveTime()

        Dim oWrite As System.IO.StreamWriter
        oWrite = File.CreateText("c:\auxtime.ini")
        Dim ctrl As Control

        For Each ctrl In Me.Controls
            If (TypeOf ctrl Is TextBox) Then
                oWrite.WriteLine(ctrl.Text)
            End If
        Next

        oWrite.Close()

    End Sub

    Public Sub Toggle(ByVal sw As Stopwatch, ByVal btn As Button, ByVal strNumber As String)

        If sw.IsRunning Then
            ' Stop the timer; show the start and reset buttons.
            sw.Stop()
            btn.Text = strNumber
            EnableButtons()
        Else
            ' Start the timer; show the stop and lap buttons.
            sw.Start()
            btn.Text = "Available"
            DisableButtons()
        End If

    End Sub

    Public Sub Ticker(ByVal sw As Stopwatch, ByVal txtbx As TextBox)

        If sw.IsRunning Then
            ' Get the elapsed time as a TimeSpan value.
            Dim ts As TimeSpan = sw.Elapsed

            ' Format and display the TimeSpan value.
            txtbx.Text = String.Format("{0:00}:{1:00}:{2:00}", _
                ts.Hours, ts.Minutes, ts.Seconds)
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Toggle(stopwatch1, Button1, "1")
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Toggle(stopwatch2, Button2, "2")
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Toggle(stopwatch3, Button3, "3")
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Toggle(stopwatch4, Button4, "4")
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Toggle(stopwatch5, Button5, "5")
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Toggle(stopwatch6, Button6, "6")
    End Sub

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Toggle(stopwatch7, Button7, "7")
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Toggle(stopwatch8, Button8, "8")
    End Sub

    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Toggle(stopwatch9, Button9, "9")
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Toggle(stopwatch10, Button10, "10")
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Ticker(stopwatch1, TextBox1)
    End Sub

    Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
        Ticker(stopwatch2, TextBox2)
    End Sub

    Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
        Ticker(stopwatch3, TextBox3)
    End Sub

    Private Sub Timer4_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer4.Tick
        Ticker(stopwatch4, TextBox4)
    End Sub

    Private Sub Timer5_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer5.Tick
        Ticker(stopwatch5, TextBox5)
    End Sub

    Private Sub Timer6_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer6.Tick
        Ticker(stopwatch6, TextBox6)
    End Sub

    Private Sub Timer7_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer7.Tick
        Ticker(stopwatch7, TextBox7)
    End Sub

    Private Sub Timer8_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer8.Tick
        Ticker(stopwatch8, TextBox8)
    End Sub

    Private Sub Timer9_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer9.Tick
        Ticker(stopwatch9, TextBox9)
    End Sub

    Private Sub Timer10_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer10.Tick
        Ticker(stopwatch10, TextBox10)
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub DisableButtons()

        Dim ctrl As Control

        For Each ctrl In Me.Controls
            If (TypeOf ctrl Is Button) Then
                If ctrl.Text <> "Available" Then
                    ctrl.Enabled = False
                End If
            End If
        Next

    End Sub

    Private Sub EnableButtons()

        Dim ctrl As Control

        For Each ctrl In Me.Controls
            If (TypeOf ctrl Is Button) Then
                ctrl.Enabled = True
            End If

        Next

    End Sub

    Private Sub reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Reset.Click

        Dim ctrl As Control

        For Each ctrl In Me.Controls
            If (TypeOf ctrl Is TextBox) Then
                ctrl.Text = "00:00:00"
            End If
        Next

        SaveTime()

    End Sub

End Class

You could also consider using a single Listview in place of all of those text boxes as well if you choose. Hope it helps.
 
Hi Applebag,

Yep, that is somewhat along the lines of what I was aiming at.
A lot of my initial code was replicated, but couldn't think of a way to call a 'general' type of sub.....like your Ticker/Toggle ones.

That certainly helps a lot.

Appreciated :D
 
As well as the streamlining done by AppleBag,

Can anyone else see a way in which I can reuse the timers/stopwatches?
i.e Use one of each for all the different 'Aux Codes', instead of 10 of each.
I realise I need to get upto scratch with OO....but for now, I'm trying to maximise code re-use where possible?

Regards
NssB
 
I wasn't quite sure what the app was meant to do, but it did seem like only one stopwath was active at any one time, correct? You could just make a general timer and stopwatch sub, and pass the number of the text box you want to report it in.

Not sure if I'm thinking your app works that way, but it's a suggestion.
 
Hi AB,

That would be the idea yeah....

But let me explain a little bit more..

The 'Aux Code' times must continue from where last left off.
i.e If 10 minutes is spent in 'Aux 4', then half an hour later, the user hits 'Aux 4' again, it must continue from the 10 minute marker....hence the reason I don't want to call stopwatch.reset().
However, you are right in saying only 1 is ever active at any given point...
So it seems a waste of resources to have 9 other timers running when they aint in use.

In my head, what I'd be doing is storing the value somewhere when the stopwatch is stopped.....and then starting the stopwatch from that point the next time it is activated for that particular 'aux code'.....
Perhaps something like a hashtable? But I'm not sure there is a method for initialising the stopwatch to start at a certain time :confused:

There is a property .... stopwatch.elapsed, just wondered if that could be set rather than queried?

Just shooting ideas out here....

Thanks for your help so far


NssB
 
Last edited:
But I'm not sure there is a method for initialising the stopwatch to start at a certain time :confused:
There isn't, store current timespan when stopped, when running a new 'session' just add current timespan with the stored one, this is also what you save next time.
 
May I interest you in some different code? See attachment, there are most likely some news for you here, but the code is really very simple and understandable. Overview:
  • class 'Tracker' keeps Name and Elapsed (StopWatch used internally) and running state.
  • a BindingList keeps any number of trackers.
  • a DataGridView display all trackers, one Timer used to refresh it. Trackers are fully controlled from the grid (add, remove, edit, start, stop)
  • the list is automatically saved/loaded with serialization when application loads/closes. Previous trackers resume elapsed time after app restart, and keep running from previous state.
 

Attachments

  • vbnet20-trackingTime.zip
    15 KB · Views: 24
Hi John,

Many thanks for your replies. I shall review the code you provided. Sounds to me like the route I need to take. And when you say the code is easy and understandable, that is open to perspective :)

I shall however get back to you if I have any problems.

Thanks again

NssB
 
Back
Top