In: Computer Science
Create an ASP.Net Website using Visual Studio with Visual Basic.Net:
Create a simple calculator that has 3 text boxes: 2 of them to enter numbers, the 3rd one displays the results
Create 4 buttons to add, subtract, multiply, and divide
Prevent the user from entering text in the number fields
Display a message indicating “cannot divide by” when the user click “/” and there is a zero the in the second box
Create two additional buttons:
- One to store data - The store data will store the results into array
- One to display data - The display data will display the contents of the array (use 10 for the array size)
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new Asp.net web site in VB is created using Visual Studio with name "CalculatorUsingVB".This website contains a web page with name "WebForm.aspx".Below are the details of this web page.
WebForm.aspx :
<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm.aspx.vb" Inherits="CalculatorUsingVB.WebForm" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<%-- title for web page --%>
<title>simple calculator using VB.net</title>
<%-- <script> is used for javascript --%>
<script>
//function to validate user inputs
function allowNumbers(e) {
var unicode=e.charCode? e.charCode : e.keyCode
if (unicode!=8)
{
//allow backspace
if (unicode<48||unicode>57)
//if not a number
{
return false //disable key
press
}
}
else
{
return true;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
Number 1:
<%-- textbox for first number --%>
<asp:TextBox ID="txtNumber1" runat="server" onkeypress="return
allowNumbers(event);"></asp:TextBox>
<br />
<br />
Number 2 :
<asp:TextBox runat="server" ID="txtNumber2" onkeypress="return
allowNumbers(event);"></asp:TextBox>
<br /> <br />
Result
:
<asp:TextBox runat="server"
ID="txtResult"></asp:TextBox>
<br />
<br />
<br />
<%-- buttons --%>
<asp:Button runat="server" ID="btnPlus" Text="+" />
<asp:Button runat="server" ID="btnMinus"
Text="-"/>
<asp:Button runat="server" ID="btnMultiply" Text="x"
/>
<asp:Button runat="server" ID="btnDivide" Text="/"
/>
<asp:Button runat="server" ID="btnStore" Text="Store"
/>
<asp:Button runat="server" ID="btnDisplay"
Text="Display" />
<br />
<br />
<%-- label ti display array data --%>
<label id="lblHeader" runat="server" />
</form>
</body>
</html>
********************************
WebForm.aspx.vb :
Public Class WebForm
Inherits System.Web.UI.Page
'declaring array to store results
Shared arr(10) As Double
'declaring variable
Shared i As Integer = 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
End Sub
'Plus button click
Protected Sub btnPlus_Click(sender As Object, e As EventArgs)
Handles btnPlus.Click
'take numbers entered by user , parse it into integer and display
result in the textbox
txtResult.Text = Integer.Parse(txtNumber1.Text) +
Integer.Parse(txtNumber2.Text)
End Sub
'Minus button click
Protected Sub btnMinus_Click(sender As Object, e As EventArgs)
Handles btnMinus.Click
'take numbers entered by user , parse it into integer and display
result in the textbox
txtResult.Text = Integer.Parse(txtNumber1.Text) -
Integer.Parse(txtNumber2.Text)
End Sub
'Multiply button click
Protected Sub btnMultiply_Click(sender As Object, e As EventArgs)
Handles btnMultiply.Click
'take numbers entered by user , parse it into integer and display
result in the textbox
txtResult.Text = Integer.Parse(txtNumber1.Text) *
Integer.Parse(txtNumber2.Text)
End Sub
'divide button click
Protected Sub btnDivide_Click(sender As Object, e As EventArgs)
Handles btnDivide.Click
'checking if second number is zero or not
If Integer.Parse(txtNumber2.Text) = 0 Then
'display message
txtResult.Text = "cannot divide by 0"
Else
'take numbers entered by user , parse it into integer and display
result in the textbox
txtResult.Text = (Integer.Parse(txtNumber1.Text) /
Integer.Parse(txtNumber2.Text)).ToString("0.00")
End If
End Sub
'store button click
Protected Sub btnStore_Click(sender As Object, e As EventArgs)
Handles btnStore.Click
arr(i) = Double.Parse(txtResult.Text) 'store result in the
array
i = i + 1 'increment value of i
End Sub
'disply button click
Protected Sub btnDisplay_Click(sender As Object, e As EventArgs)
Handles btnDisplay.Click
Dim result As String = ""
'Using for loop
For index = 0 To arr.Length - 1
'checking if result is not zero then display
If arr(index) = 0 Then
'if value is zero do nothing
Else
result = result & arr(index) & " " 'display each array
element in the label
End If
lblHeader.InnerText = result
Next
End Sub
End Class
======================================================
Output : Run application using F5 and will get the screen as shown below
Screen 1 :WebForm.aspx
Screen 2 :Screen showing addition and when display button clicked getting result from the array
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.