1. Use Option Explicit On
2. Rename your class to something meaningful
3. Before and after a sub/function there should be a new line
4. Don't put a space after the beginning of the sub
5. In VB by convention subs/function start with capital letters unless referencing a hungarian notation variable, in which case the variable should start with a capital letter after the prefix. Variables are camelcased.
6. Give things better names, for real man "button1"? At least btnSubmit
7. What happens if someone enters "one" into your textbox? I don't see any verification to make sure they are numeric
8. Not sure what "if timetable = inttextinput" is supposed to be doing.
9. Addition is faster than multiplication, why not increase efficiency a bit?
10. You should get in the habit of using On Error
11. Oh yeah And you should parse integer!
12. Watch your scoping, you don't need your variables to be class wide.
13. Personally I'd stay away from doing stuff like you do on your txtInput_Click function, I'd just make a label and write "Please enter a number between 1 and 12" there and have an empty textbox next to it.
VB code:
Option Explicit On
Public Class FiveTimesTables
Private Sub FiveTimesTables_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtInput.Text = "Please enter a number between 1 and 12"
End Sub
Private Sub txtInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtinput.Click
'this notifies the user what number they should enter
If txtInput.Text = "Please enter a number between 1 and 12" Then
txtInput.Text = ""
End If
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5Timestables.Click
On Error GoTo ErrorHandler
Dim intNumber As Integer
Dim intAnswer As Integer = 0
Dim valid As Integer = Int32.TryParse(txtInput.Text, intNumber)
If Not valid Then
MsgBox("Sorry, you must enter an integer only")
End
ElseIf intNumber > 12 Then
MsgBox("Sorry, you can only enter numbers up to 12")
End
ElseIf intNumber < 1 Then
MsgBox("Sorry, you cant enter numbers less than 1")
End
End If
For intTimesBy As Integer = 1 To 12
intAnswer += intNumber
MsgBox(intTimesBy & "x" & intNumber & "=" & intAnswer)
Next
txtInput.Text = "Please enter a number between 1 and 12"
Exit Sub
ErrorHandler:
MsgBox("An unknown error has occured")
End Sub
End Class
Basically I think it comes down to you thinking of what you would do, not of what a computer should do. Remember a computer doesn't know obvious things like "'sam' isn't a number", it will happily kill itself if you ask it to do 'sam' x 3. You should also consider what actually needs doing in loops, for example calculating number X multiplier every step of the loop is a lot more taxing than just adding the number. Also remember that in a for loop there is an index variable so you don't need to keep track yourself.
I don't know what kind of level this is, I think highschool since it's vb, but if you want to seriously write code then you should think about what kind of code you want to write. Even though you have a lot of freedom with what you can write, you should be strict with yourself.
The other day I had a vb contract and wrote about 1500 lines, I've had enough of vb for now :P It's not such a bad language though.
EDIT: By the way I didn't test anything I just wrote, so there's likely some typos.