Public Class Form1

    Private Sub Button1cmdRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1cmdRun.Click

        ' Variable declaration & initilization
        Dim hour As Integer
        Dim minute As Integer
        Dim second As Integer


        ' Validation
        If IsNumeric(txbTotal.Text) And txbTotal.Text Then >= 0 Then

            ' Total number of seconds
            Dim totalSeconds As Integer = CInt(txbTotal.Text)



            ' Calculation
            hour = totalSeconds \ 3600
            minute = (totalSeconds Mod 3600) \ 60
            second = totalSeconds Mod 60

            ' Output result
            lblResult.Text = "H: " & CStr(hour) & "  M: " & CStr(minute) & "  S: " & CStr(second)

        Else
            MessageBox.Show("Invalid input.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txbTotal.Clear()
        End If

    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txbTotal.TextChanged

    End Sub

    Private Sub lblResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblResult.Click

    End Sub
End Class

Whenever I run the program and I enter text into the label the program crashes. When I type in a negative number the message box works. How do I fix this?

Thanks.

At which line the error comes up ?

Member Avatar for MonsieurPointer

Whenever I run the program and I enter text into the label the program crashes. When I type in a negative number the message box works. How do I fix this?

Can you please tell us exactly what kind of text you tried to enter? More specifically, the text you entered which caused your program to crash and the negative number you mentioned.

A number like -45 worked and a message show appeared, whilst a string like 'bob made the program crashed.

I think line 12 is where the problem is.

Member Avatar for MonsieurPointer

I believe so, too. What are you trying to accomplish with this?

If IsNumeric(txbTotal.Text) And txbTotal.Text Then >= 0 Then

Did that even compile??

Also, when the program crashes, what kind of exception did you get? It is good programming practice to wrap code in a try-catch block as such:

Try
If IsNumeric(txbTotal.Text) And txbTotal.Text Then >= 0 Then
 
' Total number of seconds
Dim totalSeconds As Integer = CInt(txbTotal.Text)
 
 
 
' Calculation
hour = totalSeconds \ 3600
minute = (totalSeconds Mod 3600) \ 60
second = totalSeconds Mod 60
 
' Output result
lblResult.Text = "H: " & CStr(hour) & " M: " & CStr(minute) & " S: " & CStr(second)
 
Else
MessageBox.Show("Invalid input.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
txbTotal.Clear()
End If
Catch ex as Exception
' Exception handling here
End Try

try to use some more appropriate numeric checking (validation):

Dim intValue As Integer
		'value in textBox is integer
If Integer.TryParse(textBox1.Text, intValue) Then
End If

Dim decValue As Decimal
		'value is decimal (in case if you use decimals
If Decimal.TryParse(textBox1.Text, decValue) Then
End If

Part of my exercise is to validate the input. So that when a negative number or string is entered an error message appears.

Member Avatar for Unhnd_Exception

Your problem is here

' Validation        
If IsNumeric(txbTotal.Text) And txbTotal.Text >= 0 Then

Thats great your checking if the textbox's text is numeric but then you check if the textbox's text is >= 0. If its a string it will crash. You can fix this with changing And to AndAlso . That way if the Text isnot numeric the txbTotal.Text >= 0 part of the if statement will not be evaluated.

Read up on Short Circuiting.

If IsNumeric(txbTotal.Text) AndAlso Cint(txbTotal.Text) >= 0 Then
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.