Results 1 to 5 of 5

Thread: Small calculator problem in VB6

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    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

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    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).
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2008
    Posts
    3

    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

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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

  5. #5
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width