Solved

Mini Calculator in VB.NET

Posted on 2003-11-12
2
2,114 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

628 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