Solved

Mini Calculator in VB.NET

Posted on 2003-11-12
2
2,108 Views
Last Modified: 2012-06-27
Hi all:

I have codes for small calculator in VB.NET and it works just fine.  I setup three textboxes (textbox1 and textbox2 is where user enters values; the textbox3 displays result).  I have two questions:

1.  How can you code for textbox1 and textbox2 only accept numeric values?  If  user not enters numeric value, display   appropriate message to ask entering numeric values.

2.  How can you code not to allow any number devide by zero?


Please see my code below for this app

===================================================================

Dim FirstNum, SecondNum As Double
    Dim MyNumber As String

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        End
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Assign text box values to variables
        FirstNum = TextBox1.Text
        SecondNum = TextBox2.Text      
       
        'Determine checked button and calculate
       
        If RadioButton2.Checked = True Then
            TextBox3.Text = FirstNum - SecondNum
        End If
       
        If RadioButton1.Checked = True Then
            TextBox3.Text = FirstNum + SecondNum
        End If
       
        If RadioButton4.Checked = True Then
            TextBox3.Text = FirstNum / SecondNum
        End If
       
        If RadioButton3.Checked = True Then
            TextBox3.Text = FirstNum * SecondNum
        End If

    End Sub
==============================================

Thanks in advance

Larry
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click        
        TextBox1.Text = Nothing      
        TextBox2.Text = Nothing      
        TextBox3.Text = Nothing        
        TextBox1.Focus()
    End Sub
End Class
0
Comment
Question by:larryle
2 Comments
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 125 total points
ID: 9733327
the divide by zero question is easy:

If RadioButton4.Checked = True Then
      if SecondNum <> 0 then
            TextBox3.Text = FirstNum / SecondNum
      else
            MessageBox.Show("Cannot divide by zero!!!")
      end if
End If


as for the Numeric values only in the textboxes:

1)
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar.IsDigit(e.KeyChar) OrElse Asc(e.KeyChar) = Asc(".") OrElse Asc(e.KeyChar) = 8 Then
           e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar.IsDigit(e.KeyChar) OrElse Asc(e.KeyChar) = Asc(".") OrElse Asc(e.KeyChar) = 8 Then
           e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

the value 8 here is to allow the backspace key, to erase a value.

AW
0
 

Author Comment

by:larryle
ID: 9733578
Dear AW:

This program is working beautiful  now and I really much appreciate for your excellent input.

Thanks as always.

Larry
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now