First VB project done

GEC: Discuss gaming, computers and electronics and venture into the bizarre world of STGODs.

Moderator: Thanas

Post Reply
User avatar
Jaepheth
Jedi Master
Posts: 1055
Joined: 2004-03-18 02:13am
Location: between epsilon and zero

First VB project done

Post by Jaepheth »

Well, I just finished my first Visual Basic program:

http://rapidshare.com/files/219993138/10key.exe.html

It allows you to practice and evaluate 10-key performance.

It's got a lot of room for improvement, and I'm not 100% sure the KSPH is accurate, but I'm itching to move onto more ambitious projects.

Source:

Code: Select all

Public Class Form1
    Dim cnt As Integer
    Dim num As Stack(Of Integer)
    Dim clock As Double
    Dim ks As Integer
    Dim err As Integer
    Dim avg As Long
    Dim wins As Integer
    Dim fail As Boolean

    Private Sub Form1_HelpButtonClicked(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles Me.HelpButtonClicked
        MsgBox("This 10key practice program was written by" & Chr(10) & "Jeffrey Wong" & Chr(10) & "Copyright 2009" & Chr(10) & Chr(10) & "Just use the numpad to type the number displayed." & Chr(10) & "No backspacing supported in this version, sorry.", MsgBoxStyle.OkOnly)
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Timer1.Enabled = False
        newgame()
    End Sub
    Private Sub newgame()
        Label5.Text = "Press 's' to stop"
        Label4.Text = "You may begin when ready."
        Label2.Text = "KSPH:"
        Label3.Text = "Accuracy:"
        wins = 0
        fail = False
        clock = 0
        cnt = 1
        Label1.Text = 0
        num = New Stack(Of Integer)
        num.Push(0)
    End Sub
    Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        Select Case e.KeyCode
            Case Keys.NumPad0
                numpress(0)
            Case Keys.NumPad1
                numpress(1)
            Case Keys.NumPad2
                numpress(2)
            Case Keys.NumPad3
                numpress(3)
            Case Keys.NumPad4
                numpress(4)
            Case Keys.NumPad5
                numpress(5)
            Case Keys.NumPad6
                numpress(6)
            Case Keys.NumPad7
                numpress(7)
            Case Keys.NumPad8
                numpress(8)
            Case Keys.NumPad9
                numpress(9)
            Case Keys.S
                If Timer1.Enabled Then
                    Timer1.Enabled = False
                    calc()
                End If
            Case Keys.R
                If Not Timer1.Enabled Then
                    newgame()
                End If
        End Select
    End Sub
    Private Sub numpress(ByVal a)
        If Not Timer1.Enabled Then
            Timer1.Enabled = True
            Label4.Text = ""
        End If
        ks += 1
        Label4.Text = Label4.Text & a
        If Not num.Count = 0 And Not a = num.Pop() Then
            err += 1
            fail = True
        End If
        If num.Count = 0 Then
            Label4.Text = ""
            If fail And Not cnt = 1 Then
                cnt -= 1
                wins = 0
                fail = False
            End If
            If Not fail Then
                wins += 1
                If wins = 10 Then
                    Timer1.Enabled = False
                    calc()
                    MsgBox("You Win!", MsgBoxStyle.OkOnly)
                    Exit Sub
                End If
                If Not cnt = 10 And cnt = wins Then
                    cnt += 1
                    wins = 0
                End If
            End If
            newnum(cnt)
        End If
    End Sub
    Private Sub newnum(ByVal c)
        Dim rand As Random = New Random
        For i As Integer = 1 To c Step 1
            num.Push(rand.Next(0, 10))
        Next i
        Dim number As String
        Dim copy As Stack(Of Integer) = New Stack(Of Integer)
        Dim a As Integer
        For i As Integer = 1 To num.Count Step 1
            a = num.Pop()
            copy.Push(a)
            number = number & a
        Next i
        For i As Integer = 1 To copy.Count Step 1
            num.Push(copy.Pop)
        Next i
        Label1.Text = number
    End Sub
    Private Sub calc()
        Dim ksph As Double = (ks / (clock / (10 * 60 * 60)))
        Label2.Text = "KSPH: " & System.Math.Round(ksph, 3)
        Dim acc As Double = 100 * ((ks - err) / ks)
        Label3.Text = "Accuracy:  " & System.Math.Round(acc, 3) & "%"
        ks = 1
        err = 0
        avg = 0
        clock = 0
        Label5.Text = "Press 'r' to restart"
    End Sub
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        clock = clock + 1
    End Sub
End Class
Children of the Ancients
I'm sorry, but the number you have dialed is imaginary. Please rotate the phone by 90 degrees and try again.
Post Reply