Results 1 to 7 of 7

Thread: [RESOLVED] Made the Calculator. Only One character is entering in the TxtBox? How to Fix?

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    58

    Resolved [RESOLVED] Made the Calculator. Only One character is entering in the TxtBox? How to Fix?

    Hello,

    I have a keyboard only calculator coded in Java that I use for work purposes every day. Heres a link: www.reliantmfg.ca/tools/eCalc.htm

    As you can tell it is SUPER simple. one line and only accepts input from the keyboard. But it works perfectly because of its small size.

    I am looking to recreate it in VB but I have no idea where to start. I have scoured the internet for tutorials and Maybe I suck at searching but I cannot find anything that shows how to create a calculator based on keyboard input not button input.

    Any chance someone here has a link to something that shows how to make this sort of one line calculator using keyboard input only? I only need simple functions, Add subtract multiply and divide.

    Any help hint or link is greatly appreciated.

    Thankyou
    Last edited by TomToms; Sep 18th, 2014 at 02:51 PM. Reason: needed to change title to reflect what I had done so far.

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Make a Keyboard only Calculator? Cant find Info Anywhere!!

    First question would be since that is a web based calculator, are you looking to make the VB one webbased via ASP.NET or are you looking to make one in WinForms/WPF for the desktop?

    All your current one does is accept numbers being pressed when focus is in the textbox, which is how VB would do it to. It would work virtually the same way it does now from a coding perspective.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    58

    Re: Make a Keyboard only Calculator? Cant find Info Anywhere!!

    Oh, I should have verified, This wont be web based, just looking to make it VIA WinForms for desktop.

    Yup actually I want it to work exactly the same way yes. Issue is we had a guy who no longer works at this company make the Java based one and I have NO experience with we programming and very little experience in VB. (actually mostly I have done VBA in Excel which is a far shot from VB.net it seems) So Im not sure where to begin to program it.
    Heres what I have so far:
    I have made my form and I have a text box called: CalcTxtBx

    For the code of calc text box I stole some stuff from the web and have this:

    Code:
     Private Sub CalculatorLine_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CalcTxtBx.KeyPress
            If (e.KeyChar < Chr(48) Or e.KeyChar > Chr(57)) And e.KeyChar <> Chr(8) Then
                e.Handled = True
            End If
    
        End Sub
    Thats as far as I have gotten just to stop the input of words or characters. Im gonna do some more searching to make sure that the decimal is allowed as well.

    Also I noticed your sig. I am using VB 2013 Express.

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    58

    Re: Make a Keyboard only Calculator? Cant find Info Anywhere!!

    Alright so i have spent some time doing searches and coming up with stuff. Buit now I have a differnt issue. I can only type one character into the Textbox... Im not sure why. I can paste more but cant type more. Ill show you what I have so far:

    Code:
    Public Class TomsCalculator
        Dim IntCalcVal As Long
        Dim CalcAnswer As Long
        Dim ClearCalc As Integer
        Dim Operation As Integer
    
        Private Sub CalcTxtBx_KeyDown(sender As Object, e As KeyEventArgs) Handles CalcTxtBx.KeyDown
            '-- if the keycode entered is the addition button then..
            
    
            If ClearCalc = 1 Then
                If e.KeyCode = Keys.Add Then
                    CalcAnswer = IntCalcVal + Val(CalcTxtBx.Text)
                    Operation = 1
    
                ElseIf e.KeyCode = Keys.Subtract Then
                    CalcAnswer = IntCalcVal - Val(CalcTxtBx.Text)
                    Operation = 2
    
                ElseIf e.KeyCode = Keys.Divide Then
                    CalcAnswer = IntCalcVal / Val(CalcTxtBx.Text)
                    Operation = 3
    
                ElseIf e.KeyCode = Keys.Multiply Then
                    CalcAnswer = IntCalcVal * Val(CalcTxtBx.Text)
                    Operation = 4
                    
                ElseIf e.KeyCode = Keys.Enter Then
    
                    If Operation = 1 Then
                        CalcAnswer = IntCalcVal + Val(CalcTxtBx.Text)
    
                    ElseIf Operation = 2 Then
                        CalcAnswer = IntCalcVal - Val(CalcTxtBx.Text)
    
    
                    ElseIf Operation = 3 Then
                        CalcAnswer = IntCalcVal / Val(CalcTxtBx.Text)
    
    
                    ElseIf Operation = 4 Then
                        CalcAnswer = IntCalcVal * Val(CalcTxtBx.Text)
    
                    End If
                    ClearCalc = 0
                End If
    
                CalcTxtBx.Text = CalcAnswer
                CalcTxtBx.SelectionStart = 0
                CalcTxtBx.SelectionLength = Len(CalcTxtBx.Text)
                IntCalcVal = Val(CalcTxtBx.Text)
    
            Else
    
                If e.KeyCode = Keys.Add Then
                    Operation = 1
    
                ElseIf e.KeyCode = Keys.Subtract Then
                    Operation = 2
    
                ElseIf e.KeyCode = Keys.Divide Then
                    Operation = 3
    
                ElseIf e.KeyCode = Keys.Multiply Then
                    Operation = 4
    
                End If
                ClearCalc = 1
                IntCalcVal = Val(CalcTxtBx.Text)
                CalcTxtBx.Text = IntCalcVal
                CalcTxtBx.SelectionStart = 0
                CalcTxtBx.SelectionLength = Len(CalcTxtBx.Text)
            End If
    
        End Sub
    
        Private Sub CalcTxtBx_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CalcTxtBx.KeyPress
            If e.KeyChar = "."c Then
                e.Handled = (CType(sender, TextBox).Text.IndexOf("."c) <> -1)
            ElseIf e.KeyChar <> ControlChars.Back Then
                e.Handled = ("0123456789".IndexOf(e.KeyChar) = -1)
            End If
    
        End Sub
    
        Private Sub ClearBtn_Click(sender As Object, e As EventArgs) Handles ClearBtn.Click
            ClearCalc = 0
            CalcTxtBx.Text = ""
            CalcTxtBx.SelectionStart = 0
            CalcTxtBx.SelectionLength = Len(CalcTxtBx.Text)
        End Sub
    End Class
    Please by all means let me know if there is a better way to do something in there. But the main thing is where did I go wrong with the One character thing??

  5. #5
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,335

    Re: Make a Keyboard only Calculator? Cant find Info Anywhere!!

    In answer to:
    Quote Originally Posted by TomToms View Post
    But the main thing is where did I go wrong with the One character thing??
    In the KeyDown event handler, you select all the text in the textbox. At this point, the new character hasn't been added to the textbox. When it is added, it of course replaces the selected text.

  6. #6
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    Re: Made the Calculator. Only One character is entering in the TxtBox? How to Fix?

    A VB calculator is One of the most written VB applications. You saying you can not find any examples? Googling vb calculator brings up no end of examples. Is google broken your end? And don't use Val()

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2014
    Posts
    58

    Re: Make a Keyboard only Calculator? Cant find Info Anywhere!!

    Quote Originally Posted by Inferrd View Post
    In answer to:


    In the KeyDown event handler, you select all the text in the textbox. At this point, the new character hasn't been added to the textbox. When it is added, it of course replaces the selected text.
    Thankyou so much that resolved my issue.

    Also @ ident, I realize that but as I stated I was looking for a keyboard based one. Not a button based one. There are about 50000 tutorials for button based calculators but I was unable to find documentation for a keyboard based system.

    Needless to say I grabbed info from a button based one and merged that with some documentation from other places and I have completed my calculator. So this is resolved.

    Thanks to all who helped.

Tags for this Thread

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