Calculate Race winners - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

This project allow users to enter 3 names and time in seconds for each person. Then the program will determine who the first, second, third winners are.
For this tutorial you need 6 textboxes, 2 buttons:
Calculate Race winners- vb.net
Name the textboxes as follows:

txtrunner1.Text
txtrunner2.Text
txtrunner3.Text

txtRunner1Time
txtRunner2Time
txtRunner3Time

The three textboxes that will hold the three winners:
lbl1stPlace.Text
lbl2stPlace.Text
lbl3stPlace.Text
Name the buttons: btnCalculate, Clear
Calculate Race winners- vb.net
Now let's add the code for the btnCalculate click event:
Private Sub btncalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
 Handles btncalculate.Click
        Dim sngRunner1Time As String
        Dim sngRunner2Time As String
        Dim sngRunner3Time As String
        sngRunner1Time = (Val(txtRunner1Time.Text))
        sngRunner2Time = (Val(txtRunner2Time.Text))
        sngRunner3Time = (Val(txtRunner3Time.Text))
        'option 1
        If sngRunner1Time < sngRunner3Time And sngRunner1Time < sngRunner2Time _
         And sngRunner2Time < sngRunner3Time Then
            lbl1stPlace.Text = txtrunner1.Text
            lbl2ndPlace.Text = txtrunner2.Text
            lbl3rdPlace.Text = txtrunner3.Text
            'option 2
        ElseIf sngRunner1Time < sngRunner3Time And sngRunner1Time < sngRunner2Time _
         And sngRunner3Time < sngRunner2Time Then
            lbl1stPlace.Text = txtrunner1.Text
            lbl2ndPlace.Text = txtrunner3.Text
            lbl3rdPlace.Text = txtrunner2.Text
            'option3
        ElseIf sngRunner2Time < sngRunner1Time And sngRunner2Time < sngRunner3Time _
         And sngRunner1Time < sngRunner3Time Then
            lbl1stPlace.Text = txtrunner2.Text
            lbl2ndPlace.Text = txtrunner1.Text
            lbl3rdPlace.Text = txtrunner3.Text
            'option4
        ElseIf sngRunner2Time < sngRunner1Time And sngRunner2Time < sngRunner3Time _
         And sngRunner3Time < sngRunner1Time Then
            lbl1stPlace.Text = txtrunner2.Text
            lbl2ndPlace.Text = txtrunner3.Text
            lbl3rdPlace.Text = txtrunner1.Text
            'option5
        ElseIf sngRunner3Time < sngRunner1Time And sngRunner3Time < sngRunner2Time _
         And sngRunner1Time < sngRunner2Time Then
            lbl1stPlace.Text = txtrunner3.Text
            lbl2ndPlace.Text = txtrunner1.Text
            lbl3rdPlace.Text = txtrunner2.Text
            'option6
        ElseIf sngRunner3Time < sngRunner1Time And sngRunner3Time < sngRunner2Time _
         And sngRunner2Time < sngRunner1Time Then
            lbl1stPlace.Text = txtrunner3.Text
            lbl2ndPlace.Text = txtrunner2.Text
            lbl3rdPlace.Text = txtrunner1.Text
        End If
    End Sub
    

Clear button click event:
Private Sub Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
 Handles Clear.Click
        Dim a As Control
        For Each a In Me.Controls
            If TypeOf a Is TextBox Then
                a.Text = Nothing
            End If
        Next
    End Sub

Download Source Code


Share This

Home | About | Contact | Privacy Policy

Copyright visual-basic-tutorials.com 2017 - All Rights Reserved.