Hi, i'm trying to create a vb.net calcualtor similar to that of windows but with more functions, the problem is that i can't think of how i can use a single text box to get the two input numbers. i've tried using .visible but still does not work.

Dani AI

Generated

A single TextBox works fine if the app keeps the calculator state separately. Treat the TextBox as the visual "display" only and hold the operands/operator in fields (for example: operand1 As Decimal?, currentOp As String, isNewEntry As Boolean). Pressing a number either replaces or appends to the display depending on isNewEntry. Pressing an operator parses the display into the first operand, stores the chosen operator, and sets isNewEntry = True so the next digit starts the second operand. Using Decimal and Decimal.TryParse avoids common floating-point surprises.

A minimal handler pattern (adapt button names to the form) demonstrates the flow:

Private operand1 As Decimal? = Nothing
Private currentOp As String = ""
Private isNewEntry As Boolean = True

Private Sub NumberButton_Click(sender As Object, e As EventArgs)
    Dim b = DirectCast(sender, Button)
    If isNewEntry Then
        txtDisplay.Text = b.Text
        isNewEntry = False
    Else
        txtDisplay.AppendText(b.Text)
    End If
End Sub

Private Sub OperatorButton_Click(sender As Object, e As EventArgs)
    Dim val As Decimal
    If Decimal.TryParse(txtDisplay.Text, val) Then
        If operand1 Is Nothing Then
            operand1 = val
        ElseIf currentOp <> "" Then
            operand1 = Calculate(operand1.Value, val, currentOp)
            txtDisplay.Text = operand1.ToString()
        End If
        currentOp = DirectCast(sender, Button).Text
        isNewEntry = True
    End If
End Sub

Private Function Calculate(a As Decimal, b As Decimal, op As String) As Decimal
    Select Case op
        Case "+" : Return a + b
        Case "-" : Return a - b
        Case "*" : Return a * b
        Case "/" : If b = 0D Then Throw New DivideByZeroException()
                  Return a / b
        Case Else : Return b
    End Select
End Function

Keyboard input should be handled at the character level (KeyPress) so digits, backspace and the culture-specific decimal separator are accepted; map Enter to "=" in KeyDown. Common pitfalls: not resetting isNewEntry, failing to check TryParse results, and using Double when Decimal is preferable. This expands on 's two-variable idea, keeps the in-memory "cache" that hinted at, and preserves 's numeric-key approach while preferring KeyPress/KeyDown for character handling rather than relying on visible toggles.

Recommended Answers

All 3 Replies

Hi,

I did something like this myself. Use two variables (ie: Dim dblVal1 As Double, dblVal2 As Double).

Pass the 1st test entry to dblVal1, clear the text box (when the user presses a function key such as '+'), then pass the 2nd text box entry to dblVal2.

HTH, Chris.

just use cache to perform this type of operation...
It will b fine.

commented: Nonsense! -2

Adding to Numbers using your Numeric keypad VB.NET 2003
Just drag 1 label to your form
label1.backcolor = color.black
label1.forecolor = color.green
make some adjustments to your font as you want

Declare your variables

in your form use the KeyUp event
and place the code below

Private f as String 'your first number
Private s as String 'your second number
private n as String 
private o as String 'this will be your operators variable
Private sum as Double

Private Sub MainForm_KeyUp(Byval sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp

Select case e.KeyCode
 Case keys.Numpad0, keys.NumPad1 '(provide all the keycodes)
     f = e.KeyCode.ToString 'this will display what key your pressed
                f = f.Substring(6, 1) 'we will extract the last string of the keycode string
                n = n & f 'we will pass our first number to our variable n
                me.label1.Text = n 'we will display whats on variable n

Case keys.Decimal 'hmmm we will make some flavoring here
 n = n & "."
                f = ""
                n = n & f
  Case keys.Add
      o = "+" 'assign a plus sign to operators variable
                s = n 'we will pass our first number to the second number 
                sum= Val(s) + Val(r) '
                me.label1.Text = FormatNumber(sum, 2, TriState.True,  TriState.True, TriState.True)
                n = ""
                r = sum
   Case keys.Subtract
    'do your part, explore, and discover something extraordinary
    'happy coding
 End Select

so if i hopes there's nothing missing on the codes

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.