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