Small calculator problem in VB6
I`m trying to make a small calculator with + - and other buttons.
I`m stuck at the backspace button.I wrote this but if for example i press the button when no number is in display instead of not doing anything i get an error:
Private Sub Backspace_Click()
Dim LengthOfNumber As Integer
LengthOfNumber = Len(Display)
Display = Left(Display, LengthOfNumber - 1)
End Sub
And i get the error:Run-time error`5`.Invalid procedure call or argument
Re: Small calculator problem in VB6
If that code is run on the empty string, you're telling VB you want to take the leftmost -1 characters of the string, which doesn't make any sense. It's an invalid argument.
You need to add an If to check for the empty string (If Display == "" Then ... End If block).
Re: Small calculator problem in VB6
Can u please write how u mean to add it?I wrote If Display=0 then exit sub else...
and i got the error:runtime error '13' Type mismatch
Re: Small calculator problem in VB6
Type Mismatch means you are trying to use the wrong type of data (eg: String, Number, Date) for the situation.
0 is a number, but Display (assuming it is a TextBox) contains a String. To check if a String is empty, use "", eg:
Code:
If Display = "" Then Exit Sub
Note also that while you can write the code that way, it is much better (in terms of clarity and reducing errors, etc) to specify which property of the control you want to use, eg:
Code:
If Display.Text = "" Then Exit Sub
Re: Small calculator problem in VB6
The following modification should fix it.
Code:
Private Sub Backspace_Click()
Dim LengthOfNumber As Integer
LengthOfNumber = Len(Display)
If LengthOfNumber > 0 Then
Display = Left(Display, LengthOfNumber - 1)
End If
End Sub