Thursday, January 7, 2010

Intro to Visual Basic

Here is the code we did in class today. first I give you the source for the xhtml and the asp.net controls.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>

<form id="form1" runat="server">
<div>
<h1>Tip Calculator</h1>
<p>
<asp:label id="Label1" runat="server" cssclass="">Enter the Meal Amount</asp:label> <asp:textbox id="txtMeal" runat="server"></asp:textbox>
</p>
<asp:label id="Label2" runat="server" text="Choose the tip Percent"></asp:label>
<asp:radiobuttonlist id="RadioButtonList1" runat="server">
</asp:radiobuttonlist>
<asp:button id="btnSubmit" runat="server" text="Submit"></asp:button>


<asp:label id="Label3" runat="server" text="Your tip will be"></asp:label>
<asp:label id="lbltip" runat="server" text=""></asp:label>

<asp:label id="Label4" runat="server" text="Your total will be"></asp:label>
<asp:label id="lbltotal" runat="server" text=""></asp:label>
</div>
</form>
</body>
</html>



Next here is the Visual basic code. In the visual basic, in the page load event, we created an array and bound it to the radiolist controls. The If then statement checks to make sure it is not a "postback" event. That is, it is not a trip back from the server. In the button we create variables to store our values. The mealAmount value comes from the textbox on the form. The others are the result of calculations. An if then else statement checks to see which of the radiobuttons was checked and calculates the tips appropriatly. The results are displayed in label controls and formatted to currency.


Option Explicit On
Partial Class _Default
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Create an array of percents
If Not IsPostBack Then
Dim percents() As String = {"10 Percent", "15 Percent", "20 Percent"}
RadioButtonList1.DataSource = percents
RadioButtonList1.DataBind()
End If

'Dim x As Integer


End Sub





Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

If txtMeal.Text = Nothing Then
Response.Write("Enter a meal amount")
Return
End If
Dim mealAmount As Double = Double.Parse(txtMeal.Text)
Dim tip As Double
Dim total As Double


If RadioButtonList1.SelectedItem.Text = "10 Percent" Then
tip = mealAmount * 0.1
ElseIf RadioButtonList1.SelectedItem.Text = "15 Percent" Then
tip = mealAmount * 0.15
Else
tip = mealAmount * 0.2
End If


total = mealAmount + tip


lbltip.Text = tip.ToString("$#,##0.00")
lbltotal.Text = total.ToString("$#,##0.00")


End Sub
End Class

2 comments:

  1. Would it be possible to have a cover page with links to each class? As you add data for each class, it makes it a long scroll to find the class needed.

    Thank you!

    ReplyDelete