I am taking my first class (online) in Visual Basic and we are using VB 2010. The assignment is to create a football calculator(see attached). I have attached the instructions that the teacher gave us and I think that I created the user interface okay but I don't understand about how to declare variables and how to calculate. I also don't understand what a LostFocus event is. I am also not sure if I should use regular text boxes or masked text boxes. Any guidance would be much appreciated as I want to learn this, it's just the book doesn't seem to explain this very well and I have searched for help on calculating but it seems the examples are showing as if you have a calculate button.

Help :'(

Recommended Answers

All 3 Replies

See if this helps.
2 TextBoxes, 1 Label

Public Class Form1
    Private iTeam1Total As Integer = 0 '// Declaration.

    '// Use the _TextChanged Event. Can add all TextBoxes that are for the same team, as the commented TextBox3.TextChanged.
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                        Handles TextBox1.TextChanged, TextBox2.TextChanged ',TextBox3 .TextChanged '// etc.
        Try '//---- Catch errors to not crash the app. if not a Numeric value in TextBoxes.
            '// Add to your Team's Total.
            iTeam1Total = CInt(TextBox1.Text) + CInt(TextBox2.Text) '// CInt = Convert to Integer, since .Text is a String.
        Catch ex As Exception
        End Try '---\\
        Label1.Text = iTeam1Total '// Display Result.
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.Text = "0" : TextBox2.Text = "0"
    End Sub
End Class

LostFocus is when a Control looses Focus.
For example, you click TextBox1, then click TextBox2. TextBox1 will loose Focus and if you have anything in the TextBox1_LostFocus Event, that event will fire with the code within.

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        MsgBox("TextBox1 Lost Focus", MsgBoxStyle.Information)
    End Sub

As for debating over TextBoxes, try the different controls on your own and see which would benefit your project the best.:)

Public Class Donna 's Football Calculator
'Declare module level variables and contants
Private iTeam1Total As Integer = 0
'Add all TextBoxes that are for the same team. Use the _TextChanged Event.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged, TextBox3.TextChanged, TextBox4.TextChanged
Try 'Catch errors to not crash the app. if not a Numeric value in TextBoxes.
'Add to your Team's Total. CInt = Convert to Integer, since .Text is a String.
iTeam1Total = CInt(TextBox1.Text) + CInt(TextBox2.Text) + CInt(TextBox3.Text) + CInt(TextBox4.Text)
Catch ex As Exception
End Try

'Display Results
Label1.Text = CStr(iTeam1Total)
End Sub
Private iTeam2Total As Integer = 0
'Add all TextBoxes that are for the same team. Use the _TextChanged Event.

Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged, TextBox7.TextChanged, TextBox8.TextChanged
Try 'Catch errors to not crash the app. if not a Numeric value in TextBoxes.
'Add to your Team's Total. CInt = Convert to Integer, since .Text is a String.
iTeam1Total = CInt(TextBox1.Text) + CInt(TextBox2.Text) + CInt(TextBox3.Text) + CInt(TextBox4.Text)
Catch ex As Exception
End Try

'Display Results
Label2.Text = CStr(iTeam2Total)

End Sub
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
MsgBox("TextBox1 Lost Focus", MsgBoxStyle.Information)
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0" : TextBox2.Text = "0" : TextBox3.Text = "0" : TextBox4.Text = "0"
End Sub

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

Not sure if you noticed, but you have the same code in: Private Sub TextBox5_TextChanged(... for team 2 as you do for team 1 in: Private Sub TextBox1_TextChanged(... . Both add up a total for team 1.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.