Solved

Mini Calculator in VB.NET

Posted on 2003-11-12
2
2,110 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

786 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