Jump to content

How did I do in my Stopwatch Program? Visual Basic

I decided I wanted to make a stopwatch, and a basic one in VB. Here is what the program looks like:

sw.jpg

 

Public Class Form1    Dim Seconds As Integer    Dim sraw As Decimal    Dim mraw As Decimal    Dim hraw As Decimal    Dim sround As String    Dim mround As String    Dim hround As String    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Timer1.Enabled = True    End Sub    Private Sub Button3_Click(sender As Object, e As EventArgs)        Timer1.Enabled = True    End Sub    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click        Timer1.Enabled = False    End Sub    Private Sub Button4_Click(sender As Object, e As EventArgs)        Timer1.Enabled = False    End Sub    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick        Seconds = Seconds + 1            ' Second Determine        sraw = Seconds - 60 * Math.Floor(mraw)            mraw = Seconds / 60        hraw = Seconds / 3600        If sraw = 60 Then            sround = "00"        Else            If sround < 9 Then                sround = "0" & sraw            Else                sround = sraw            End If        End If        If mraw = 60 Then            mround = "00"        Else            If mround < 9 Then                mround = "0" & Math.Floor(mraw) - 60 * Math.Floor(hraw)            Else                mround = Math.Floor(mraw) - 60 * Math.Floor(hraw)            End If        End If        If hraw = 60 Then            hround = "00"        Else            If hround < 9 Then                hround = "0" & Math.Floor(hraw)            Else                hround = Math.Floor(hraw)            End If        End If        Label1.Text = hround & ":" & mround & ":" & sround    End Sub    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click        Seconds = 0        Label1.Text = "00:00:00"    End SubEnd Class
Outside of this there is a timer running at an interval of 1000 ms. How good did I do? What ways could I improve on it?
Link to post
Share on other sites

You should make the reset button bigger. Looks goofy.

Main rig on profile

VAULT - File Server

Spoiler

Intel Core i5 11400 w/ Shadow Rock LP, 2x16GB SP GAMING 3200MHz CL16, ASUS PRIME Z590-A, 2x LSI 9211-8i, Fractal Define 7, 256GB Team MP33, 3x 6TB WD Red Pro (general storage), 5x 8TB WD White Label/Red (Plex) (both arrays in their respective Windows Parity storage spaces), 1TB Teamgroup MP33 (dumping ground) Corsair RM750x, TrueNAS Scale

Sleeper HP Pavilion A6137C

Spoiler

Intel Core i7 6700K @ 4.4GHz, 4x8GB G.SKILL Ares 1800MHz CL10, ASUS Z170M-E D3, 128GB Team MP33, 1TB Seagate Barracuda, MSI GTX 970 100ME, EVGA 650G1, Windows 11 Pro

OptiPlex 7040M

Spoiler

Intel Core i7 6700, 2x16GB Mushkin Redline (stuck at 2133MHz CL13), 240GB Corsair MP510, 2TB Seagate Barracuda 2.5", 130w Dell power brick, Windows 11 Pro

Mac Mini (Late 2020)

Spoiler

Apple M1, 8GB RAM, 256GB, macOS Sonoma

Consoles: Steam Deck LCD (512GB), Softmodded 1.4 Xbox w/ 500GB HDD, Xbox 360 Elite 120GB Falcon, XB1X w/2TB MX500, Xbox Series X, PS1 1001, PS2 Slim 70000 w/ FreeMcBoot, PS4 Pro 7015B 1TB, PS5 Digital, Nintendo Switch OLED, Nintendo Wii RVL-001 (black)

Link to post
Share on other sites

another way of dealing with the leading zeroes without doing all those if-elses is to use the ToString method of the Integer type with the appropriate parameter

hrow.ToString("D2");

returns the decimal (D) representation of hrow, with at least 2 digits

 

and in regards of how to deal with time, some different maths could make it easier as well

Dim sec As IntegerDim min  As IntegerDim hours As IntegerDim result As Stringsec = Seconds Mod 60min = (Seconds \ 60 ) Mod 60hours = Seconds \ 3600result = sec.ToString("D2") & " : " & min.ToString("D2") & " : " & hours.ToString("D2)

(hopefully this works)

 

just keep in mind that thinking about the code immediately could lead you to do "bad" code

if you plan it taking some more time, you could find simpler/more efficient/more elegant ways of doing the same thing

Link to post
Share on other sites

another way of dealing with the leading zeroes without doing all those if-elses is to use the ToString method of the Integer type with the appropriate parameter

hrow.ToString("D2");
returns the decimal (D) representation of hrow, with at least 2 digits

 

and in regards of how to deal with time, some different maths could make it easier as well

Dim sec As IntegerDim min  As IntegerDim hours As IntegerDim result As Stringsec = Seconds Mod 60min = (Seconds \ 60 ) Mod 60hours = Seconds \ 3600result = sec.ToString("D2") & " : " & min.ToString("D2") & " : " & hours.ToString("D2)
(hopefully this works)

 

just keep in mind that thinking about the code immediately could lead you to do "bad" code

if you plan it taking some more time, you could find simpler/more efficient/more elegant ways of doing the same thing

 

Damn... I made it so much more complex than it needed to be.

My code is now this:

 

Public Class Form1    Dim Seconds As Integer    Dim sec As Integer    Dim min As Integer    Dim hours As Integer    Dim result As String    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Timer1.Enabled = True    End Sub    Private Sub Button3_Click(sender As Object, e As EventArgs)        Timer1.Enabled = True    End Sub    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click        Timer1.Enabled = False    End Sub    Private Sub Button4_Click(sender As Object, e As EventArgs)        Timer1.Enabled = False    End Sub    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick        Seconds = Seconds + 1        sec = Seconds Mod 60        min = (Seconds \ 60) Mod 60        hours = Seconds \ 3600        result = hours.ToString("D2") & ":" & min.ToString("D2") & ":" & sec.ToString("D2")        Label1.Text = result    End Sub    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click        Seconds = 0        Label1.Text = "00:00:00"    End SubEnd Class

For now the program still looks the same as above.

Link to post
Share on other sites

Made some more progress.

sw2.jpg

And the code

Public Class Form1    Dim Seconds As Integer    Dim sec As Integer    Dim min As Integer    Dim hours As Integer    Dim result As String    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        If Timer1.Enabled = True Then            Button1.Text = "Start"            Timer1.Enabled = False        Else            Button1.Text = "Stop"            Timer1.Enabled = True        End If    End Sub    Private Sub Button3_Click(sender As Object, e As EventArgs)         End Sub    Private Sub Button4_Click(sender As Object, e As EventArgs)        Timer1.Enabled = False    End Sub    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick        Seconds = Seconds + 1        sec = Seconds Mod 60        min = (Seconds \ 60) Mod 60        hours = Seconds \ 3600        result = hours.ToString("D2") & ":" & min.ToString("D2") & ":" & sec.ToString("D2")        Label1.Text = result    End Sub    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click        Seconds = 0        Label1.Text = "00:00:00"    End Sub    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load        Me.Size = New System.Drawing.Size(253, 228)    End Sub    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click        If Me.Size.Height = 228 Then            Me.Size = New System.Drawing.Size(253, 365)            Button2.Text = "/\ Options /\"        Else            Me.Size = New System.Drawing.Size(253, 228)            Button2.Text = "\/ Options \/"        End If    End Sub    Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick        If CheckBox1.Checked = True Then            Me.TopMost = True        Else            Me.TopMost = False        End If    End SubEnd Class

@tomlambert01

Any other suggestions for the options? 

 

@tmcclelland455

Look better now?

Link to post
Share on other sites

Nope, it looks good, very good. I'm impressed.

 

I need to learn C#, it's the language of industry and pretty much everything. I know a bit of C++, although it's rather going out of fashion.

C++ going out of fashion? That entirely depends what you're doing.

As a Windows Driver developer I can assure you that C++ is very much in popular use!

Link to post
Share on other sites

  • 1 month later...

Hmm I don't know exactly what or where this issue is because I didn't look at code but I saw something about leading 0s. Since it looks like most things in the program that are displayed are strings I would use the string.trim or trimleft or if needed trimright.

Link to post
Share on other sites

The single biggest problem in communication is the illusion that it has taken place.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×