Solved

Mini Calculator in VB.NET

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…

821 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