Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Mini Calculator in VB.NET

Posted on 2003-11-12
2
Medium Priority
?
2,119 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 500 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

824 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