0% found this document useful (0 votes)
5 views

5. Practicals(Application Development using .Net Technology Framework)

Uploaded by

yashoda sailwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

5. Practicals(Application Development using .Net Technology Framework)

Uploaded by

yashoda sailwal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Practical 9. Create A Project That Calculates the Total of Fat, Carbohydrate and Protein.

Allow The
User To Enter Into Text Boxes. the Grams of Fat, Grams Of Carbohydrate And Grams Of
Protein. Each Gram of Fat Is 9 Calories and Protein Or Carbohydrate Is Calories. Display the Total
Calories of The Current Food Item In A Label. Use To Other Labels To Display And Accumulated
Some Of Calories And The Count Of Items Entered. The form food have 3 text boxes For The User
To Enter The Grams For Each Category Include Label Next To Each Text Box Indicating What
The User Is Enter.

Solution:

Steps:

1. Open Visual Studio or any other C# IDE.

2. Create a new Windows Forms Application project.

3. Design your form with the following elements:

- Three Label controls to indicate what each TextBox is for (e.g., "Grams of Fat:", "Grams
of Protein:", "Grams of Carbohydrates:").

- Three TextBox controls for users to enter the grams of fat, protein, and carbohydrates.

- One Button control to calculate and update the total calories.

- Three Label controls to display the calculated calories for fat, protein, and carbohydrates.

- Three more Label controls to display the accumulated calories for each category.

- One Label control to display the total accumulated calories.

- One Label control to display the count of items entered.

- Make sure to arrange these controls in a clear and organized manner on your form.

4. Write the C# code to calculate the total calories, update the accumulated calories, and
count of items entered. Here's a sample code for your form:

```csharp

using System;

using System.Windows.Forms;
namespace FoodCalorieCalculator

public partial class Form1 : Form

private double totalCalories = 0;

private int itemCount = 0;

public Form1()

InitializeComponent();

private void CalculateCaloriesButton_Click(object sender, EventArgs e)

// Get user-entered grams for fat, protein, and carbohydrates

double gramsOfFat = double.Parse(FatTextBox.Text);

double gramsOfProtein = double.Parse(ProteinTextBox.Text);

double gramsOfCarbohydrates = double.Parse(CarbohydratesTextBox.Text);

// Calculate calories for each category

double fatCalories = gramsOfFat * 9; // Each gram of fat is 9 calories

double proteinCalories = gramsOfProtein * 4; // Each gram of protein is 4 calories

double carbCalories = gramsOfCarbohydrates * 4; // Each gram of carbohydrate is 4


calories

// Update Labels to display calories for each category


FatCaloriesLabel.Text = $"Fat Calories: {fatCalories}";

ProteinCaloriesLabel.Text = $"Protein Calories: {proteinCalories}";

CarbohydratesCaloriesLabel.Text = $"Carb Calories: {carbCalories}";

// Calculate and update accumulated calories and item count

totalCalories += fatCalories + proteinCalories + carbCalories;

AccumulatedCaloriesLabel.Text = $"Accumulated Calories: {totalCalories}";

itemCount++;

ItemCountLabel.Text = $"Item Count: {itemCount}";

// Update the total accumulated calories label

TotalCaloriesLabel.Text = $"Total Calories: {totalCalories}";

```

5. Set the event handler for the "Calculate Calories" button's click event. You can do this by
double-clicking the button in the form designer, which will generate the
`CalculateCaloriesButton_Click` event handler.

6. Build and run your application to test the food calorie calculator.

Practical 10. Create The Web Application That Accepts Name, Password ,Age , Email I‹:1, And
User Id. All The Information Entry Is Compulsory. Password Should Be Reconfirmed. Age
Should Be Within 21 To 30. Email Id Should Be Valid. User Id Should Have At Least A Capital Letter
And Digit As Well As Length Should Be Between 7 And 20 Characters.

Solution:

To create a web application in C# .NET that accepts user information (Name, Password, Age,
Email, and User ID) with validation rules, you can follow these steps:
1. Open Visual Studio or any other C# IDE.

2. Create a new ASP.NET Web Application project.

3. Design your web form with the following elements:

- Five Label controls to indicate each input field (Name, Password, Confirm Password,
Age, Email, User ID).

- Five TextBox controls for users to enter their information.

- One Button control to submit the information.

- Make sure to arrange these controls in a clear and organized manner on your web form.

4. Add validation controls to enforce the validation rules for each input field. You can use
RequiredFieldValidator, RangeValidator, RegularExpressionValidator, and CustomValidator
controls for different validation tasks.

Here's a sample code for your web form's HTML (WebForm1.aspx):

```html

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebForm1.aspx.cs"


Inherits="WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

<title>Registration Form</title>

</head>

<body>
<form id="form1" runat="server">

<div>

<h2>Registration Form</h2>

<asp:Label ID="NameLabel" runat="server" Text="Name:"


CssClass="label"></asp:Label>

<asp:TextBox ID="NameTextBox" runat="server"


CssClass="textbox"></asp:TextBox>

<asp:RequiredFieldValidator ID="NameValidator" runat="server"


ControlToValidate="NameTextBox" ErrorMessage="Name is required" Display="Dynamic"
ForeColor="Red"></asp:RequiredFieldValidator>

<br /><br />

<asp:Label ID="PasswordLabel" runat="server" Text="Password:"


CssClass="label"></asp:Label>

<asp:TextBox ID="PasswordTextBox" runat="server" TextMode="Password"


CssClass="textbox"></asp:TextBox>

<asp:RequiredFieldValidator ID="PasswordValidator" runat="server"


ControlToValidate="PasswordTextBox" ErrorMessage="Password is required"
Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator>

<br /><br />

<asp:Label ID="ConfirmPasswordLabel" runat="server" Text="Confirm Password:"


CssClass="label"></asp:Label>

<asp:TextBox ID="ConfirmPasswordTextBox" runat="server"


TextMode="Password" CssClass="textbox"></asp:TextBox>

<asp:RequiredFieldValidator ID="ConfirmPasswordValidator" runat="server"


ControlToValidate="ConfirmPasswordTextBox" ErrorMessage="Confirm Password is
required" Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator>

<asp:CompareValidator ID="PasswordMatchValidator" runat="server"


ControlToValidate="ConfirmPasswordTextBox" ControlToCompare="PasswordTextBox"
Operator="Equal" Type="String" ErrorMessage="Passwords must match"
Display="Dynamic" ForeColor="Red"></asp:CompareValidator>

<br /><br />

<asp:Label ID="AgeLabel" runat="server" Text="Age:"


CssClass="label"></asp:Label>

<asp:TextBox ID="AgeTextBox" runat="server"


CssClass="textbox"></asp:TextBox>

<asp:RangeValidator ID="AgeValidator" runat="server"


ControlToValidate="AgeTextBox" Type="Integer" MinimumValue="21"
MaximumValue="30" ErrorMessage="Age must be between 21 and 30" Display="Dynamic"
ForeColor="Red"></asp:RangeValidator>

<br /><br />

<asp:Label ID="EmailLabel" runat="server" Text="Email:"


CssClass="label"></asp:Label>

<asp:TextBox ID="EmailTextBox" runat="server"


CssClass="textbox"></asp:TextBox>

<asp:RegularExpressionValidator ID="EmailValidator" runat="server"


ControlToValidate="EmailTextBox" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\
w+)*\.\w+([-.]\w+)*" ErrorMessage="Invalid email address" Display="Dynamic"
ForeColor="Red"></asp:RegularExpressionValidator>

<br /><br />

<asp:Label ID="UserIdLabel" runat="server" Text="User ID:"


CssClass="label"></asp:Label>

<asp:TextBox ID="UserIdTextBox" runat="server"


CssClass="textbox"></asp:TextBox>
<asp:RequiredFieldValidator ID="UserIdValidator" runat="server"
ControlToValidate="UserIdTextBox" ErrorMessage="User ID is required"
Display="Dynamic" ForeColor="Red"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="UserIdPatternValidator" runat="server"


ControlToValidate="UserIdTextBox" ValidationExpression="^(?=.*[A-Z])(?=.*\d).{7,20}$"
ErrorMessage="User ID must have at least one capital letter, one digit, and be between 7 and
20 characters" Display="Dynamic" ForeColor="Red"></asp:RegularExpressionValidator>

<br /><br />

<asp:Button ID="SubmitButton" runat="server" Text="Submit"


OnClick="SubmitButton_Click" CssClass="button" />

<br /><br />

<asp:Label ID="ResultLabel" runat="server" CssClass="result"


Text=""></asp:Label>

</div>

</form>

</body>

</html>

```

5. In the code-behind (WebForm1.aspx.cs), add the event handler for the Submit button:

```csharp

using System;

using System.Web.UI;

public partial class WebForm1 : Page


{

protected void SubmitButton_Click(object sender, EventArgs e)

if (Page.IsValid)

// All validation rules passed, process the form data

string name = NameTextBox.Text;

string password = PasswordTextBox.Text;

int age = Convert.ToInt32(AgeTextBox.Text);

string email = EmailTextBox.Text;

string userId = UserIdTextBox.Text;

// You can perform further actions with the collected data here

// Display a success message

ResultLabel.Text = "Registration successful!";

else

// Display an error message

ResultLabel.Text = "Registration failed. Please check your input.";

```

6. Build and run your ASP.NET web application to test the registration form with validation.

You might also like