Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a calculator using VB.NET; but I can't make it work with number pad. I really appreciate your help. Thanks
Posted
Updated 2-May-12 5:56am
v2

Are you sure you've kept Num Lock ON for enabling Number pad inputs?

or I am missing something ? please elaborate question in detail for getting more suitable answers.
 
Share this answer
 
Comments
calvin ruka 10-Jan-11 4:29am    
I have try in many ways. As i know, my coding is causing problem but I don't know which part is the problem. >.<
I think you might have used KeyCode in KeyDown even, like
e.KeyCode = Windows.Forms.Keys.D2

where D2 is different from NumPad2.

Try using KeyChar in KeyPress event.

Share some code if this doesn't help.
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 2-May-12 11:59am    
A good technical answer.
My +5
All you do is hit the NumLock key on your keyboard (the light should turn on.) That's it, nothing needs to be done in your code at all.
 
Share this answer
 
Comments
calvin ruka 10-Jan-11 10:16am    
Thank You for reply me. I have try hit the NumLock key to make it ON/ Green light ON. Still there is no any respond at all.
Dave Kreskowiak 10-Jan-11 11:56am    
Without seeing your code, it's impossible to tell what you've done wrong.
Shahin Khorshidnia 2-May-12 12:00pm    
Good point.
try it

VB
Dim s As String
Dim kflag As Boolean
Dim a, b As Double



Private Sub Command1_Click()
Text1.Text = Text1.Text + "1"
End Sub

Private Sub Command10_Click()
Text1.Text = Text1.Text + "7"
End Sub

Private Sub Command11_Click()
Text1.Text = Text1.Text + "8"
End Sub

Private Sub Command12_Click()
Text1.Text = Text1.Text + "9"
End Sub

Private Sub Command13_Click()
Text1.Text = Text1.Text + "0"
End Sub

Private Sub Command14_Click()

a = Val(Text1.Text)
Text1.Text = ""
Text1.Text = a - Val(Text1.Text)
Text1.Text = ""
s = "-"
End Sub

Private Sub Command15_Click()


If a = 0 Then a = 1




a = a * Val(Text1.Text)
Text1.Text = ""
s = "*"
End Sub

Private Sub Command16_Click()
If a = 0 Then a = 1
a = a \ Val(Text1.Text)
a = Val(Text1.Text)
Text1.Text = ""


s = "\"
End Sub

Private Sub Command17_Click()
Text1.Text = 0
Text1.Text = ""

End Sub

Private Sub Command2_Click()
Text1.Text = Text1.Text + "2"
End Sub




Private Sub Command3_Click()
Text1.Text = a + Val(Text1.Text)
a = Val(Text1.Text)
Text1.Text = ""
s = "+"



End Sub

Private Sub Command4_Click()
If s = "+" Then
Text1.Text = Val(Text1.Text) + a
ElseIf s = "*" Then
Text1.Text = Val(Text1.Text) * a
ElseIf s = "-" Then
Text1.Text = a - Val(Text1.Text)
ElseIf s = "\" Then
Text1.Text = a \ Val(Text1.Text)
End If
a = Val(Text1.Text)
End Sub

Private Sub Command5_Click()
If Text1.Text = o Then
Text1.Text = "0."
ElseIf Text1.Text <> 0 Then
Text1.Text = Text1.Text + "."
End If

End Sub

Private Sub Command6_Click()
Text1.Text = Text1.Text + "3"
End Sub

Private Sub Command7_Click()
Text1.Text = Text1.Text + "4"
End Sub

Private Sub Command8_Click()
Text1.Text = Text1.Text + "5"
End Sub

Private Sub Command9_Click()
Text1.Text = Text1.Text + "6"
End Sub

Private Sub Form_Load()
Dim a, b As Double
kflag = True
End Sub
 
Share this answer
 
VB
Option Explicit On
Imports VB = Microsoft.VisualBasic

Public Class Form1
    Private number1 As Double
    Private number2 As Double
    Private input As Boolean
    Private calfun As String
    Private demical As String


  Private Sub Form1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress

        If e.KeyChar >= "0" And e.KeyChar <= "9" Then


        End If

    End Sub






    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

    Private Sub B1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B1.Click
        If input = False Then
            display.Text += B1.Text
        Else
            display.Text = B1.Text
            input = False
        End If
    End Sub

    Private Sub B2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B2.Click
        If input = False Then
            display.Text += B2.Text
        Else
            display.Text = B2.Text
            input = False
        End If
    End Sub

    Private Sub B3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B3.Click
        If input = False Then
            display.Text += B3.Text
        Else
            display.Text = B3.Text
            input = False
        End If
    End Sub

    Private Sub B4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B4.Click
        If input = False Then
            display.Text += B4.Text
        Else
            display.Text = B4.Text
            input = False
        End If
    End Sub

    Private Sub B5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B5.Click
        If input = False Then
            display.Text += B5.Text
        Else
            display.Text = B5.Text
            input = False
        End If
    End Sub

    Private Sub B6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B6.Click
        If input = False Then
            display.Text += B6.Text
        Else
            display.Text = B6.Text
            input = False
        End If
    End Sub

    Private Sub B7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B7.Click
        If input = False Then
            display.Text += B7.Text
        Else
            display.Text = B7.Text
            input = False
        End If
    End Sub

    Private Sub B8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B8.Click
        If input = False Then
            display.Text += B8.Text
        Else
            display.Text = B8.Text
            input = False
        End If
    End Sub

    Private Sub B9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B9.Click
        If input = False Then
            display.Text += B9.Text
        Else
            display.Text = B9.Text
            input = False
        End If
    End Sub

    Private Sub B0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles B0.Click
        If input = False Then
            If display.Text.Length >= 1 Then
                display.Text += B0.Text
            End If
        End If
    End Sub

    Private Sub dot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dot.Click
        If display.Text = "" Then
            display.Text = "0."
        Else
            demical = IIf(InStr(display.Text, ".") > 0, "", ".")
            display.Text = display.Text & demical
            If VB.Left(display.Text, 1) = "0" Then
                display.Text = Mid(display.Text, 2)
            End If
        End If
        If display.Text = "." Then display.Text = "0."
    End Sub

    Private Sub clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clear.Click
        display.Text = String.Empty
        number1 = 0
        number2 = 0
        calfun = String.Empty
        demical = False
    End Sub

    Private Sub plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles plus.Click
        If display.Text.Length <> 0 Then
            If calfun = String.Empty Then
                number1 = CDbl(display.Text)
                display.Text = String.Empty
            Else
                CalculateTotals()
            End If
            calfun = "plus"
            demical = False
        End If
    End Sub

    Private Sub minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles minus.Click
        If display.Text.Length <> 0 Then
            If calfun = String.Empty Then
                number1 = CDbl(display.Text)
                display.Text = String.Empty
            Else
                CalculateTotals()
            End If
            calfun = "minus"
            demical = False
        End If
    End Sub

    Private Sub times_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles times.Click
        If display.Text.Length <> 0 Then
            If calfun = String.Empty Then
                number1 = CDbl(display.Text)
                display.Text = String.Empty
            Else
                CalculateTotals()
            End If
            calfun = "times"
            demical = False
        End If
    End Sub

    Private Sub devide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles devide.Click
        If display.Text.Length <> 0 Then
            If calfun = String.Empty Then
                number1 = CDbl(display.Text)
                display.Text = String.Empty
            Else
                CalculateTotals()
            End If
            calfun = "devide"
            demical = False
        End If
    End Sub


    Private Sub equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equal.Click
        If display.Text.Length <> 0 AndAlso number1 <> 0 Then
            CalculateTotals()
            calfun = ""
            demical = False
        End If
    End Sub


    Private Sub CalculateTotals()
        number2 = CDbl(display.Text)
        Select Case calfun
            Case "plus"
                number1 = number1 + number2
            Case "minus"
                number1 = number1 - number2
            Case "times"
                number1 = number1 * number2
            Case "devide"
                number1 = number1 / number2
        End Select
        display.Text = CStr(number1)
        input = True
    End Sub

End Class


Here is my code. Hope u have clue to let me know where should i edit, add or change.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 18-Jan-11 16:02pm    
For example, you repeated identical code of numeric button processing some 10 times. If should never be done. Learn what's a function, parameter, variable and their use before going forward. From this moment, it's time to through out you code and start over. At the moment, you're not ready for a project like that. You really need to learn basic techniques on simple samples of few lines each, read some books.
Maciej Los 2-May-12 11:21am    
---=== comment deleted ===---
Code Master38 2-May-12 11:26am    
That is ONE piece of scary coding...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900