0% found this document useful (0 votes)
38 views17 pages

(20-Arid-811) (Mustajab Ahmed) (Assignment-02)

The document contains the code and screenshots for 9 questions on visual programming in C#. The questions cover topics like reading patient names, calculating factorials, building basic forms to calculate area and time differences, and using controls like radio buttons, checkboxes, datetime pickers and mask text boxes.

Uploaded by

Mustajab Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views17 pages

(20-Arid-811) (Mustajab Ahmed) (Assignment-02)

The document contains the code and screenshots for 9 questions on visual programming in C#. The questions cover topics like reading patient names, calculating factorials, building basic forms to calculate area and time differences, and using controls like radio buttons, checkboxes, datetime pickers and mask text boxes.

Uploaded by

Mustajab Ahmed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 17

ASSIGNMENT no 02

COURSE NAME: Visual Programming

DEGREE: BS (SE) - 7B / MORNING

SUBMITTED TO: Dr. Saif Ur Rehman

SUBMITTION DATE: 06-November-2023

SUBMITTED BY: Mustajab Ahmed

ARID NO: 20-ARID-811


Question No. 1 Write C# Code to read ten Patients Name and then display the names in
alphabetically (A to Z)
Answer:
Code:
using System;
using System.Collections.Generic;

class Program
{
static void Main()
{
List<string> patientNames = new List<string>();
Console.WriteLine("Enter ten patient names:");
for (int i = 1; i <= 10; i++)
{
Console.Write($"Enter Patient Name {i}: ");
string name = Console.ReadLine();
patientNames.Add(name);
}
patientNames.Sort();
Console.WriteLine("\nPatient Names:");
for (int i = 0; i < patientNames.Count; i++)
{
Console.WriteLine($"Input: {i + 1} - {patientNames[i]}");
}
}
}
ScreenShot:
Question 02:
Code:
using System;
using System.Windows.Forms;

namespace WinFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btnCalculate_Click(object sender, EventArgs e)


{
if (int.TryParse(textBox1.Text, out int number))
{
long factorial = CalculateFactorial(number);
MessageBox.Show($"Factorial of {number} is {factorial}", "Factorial Result");
}
else
{
MessageBox.Show("Please enter a valid number.", "Error");
}
}

private long CalculateFactorial(int n)


{
if (n == 0)
return 1;

long result = 1;
for (int i = 1; i <= n; i++)
{
result *= i;
}
return result;
}
}
}

Screen Shot:
Question 03:
Write the code for the following design form, design this form in your Visual Studio and then
write complete code using C# programming language.
Code:
using System;
using System.Windows.Forms;

namespace WinFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{

}
private void btnCalculate_Click(object sender, EventArgs e)
{
if (double.TryParse(textBox1.Text, out double num1) &&
double.TryParse(textBox2.Text, out double num2))
{
double sum = num1 + num2;
resultLabel.Text = $"Sum: {sum}";
}
else
{
resultLabel.Text = "Please enter valid numbers in both fields.";
}

}
}
}
ScreeenShot:
Question: 04
Code:
using System;
using System.Windows.Forms;

namespace WinFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
}

private void btnCalculate_Click(object sender, EventArgs e)


{
if (double.TryParse(textBox1.Text, out double length) &&
double.TryParse(textBox2.Text, out double width))
{
double area = length * width;
double perimeter = 2 * (length + width);
resultLabel.Text = $"Area: {area}\nPerimeter: {perimeter}";
}
else
{
resultLabel.Text = "Please enter valid numbers for Length and Width.";
}
}
}
}

ScreenShot:

Question:05
Code:
using System;
using System.Windows.Forms;

namespace WinFormsApp3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
sexComboBox.Items.Add("Male");
sexComboBox.Items.Add("Female");
}

private void Form1_Load(object sender, EventArgs e)


{
}

private void btnSubmit_Click(object sender, EventArgs e)


{
string patientName = patientNameTextBox.Text;
string age = ageTextBox.Text;
string address = addressTextBox.Text;
string contactNumber = contactNumberTextBox.Text;
string emergencyContact = emergencyContactTextBox.Text;
string sex = sexComboBox.SelectedItem != null ?
sexComboBox.SelectedItem.ToString() : "Not specified";

string patientInfo = $"Patient Name: {patientName}\n" +


$"Age: {age}\n" +
$"Address: {address}\n" +
$"Contact Number: {contactNumber}\n" +
$"Emergency Contact Number: {emergencyContact}\n" +
$"Sex: {sex}";

MessageBox.Show(patientInfo, "Patient Information");


}
}
}

ScreeenShot:
Question: 06
Code:
using System;
using System.Windows.Forms;

namespace WinFormsApp4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
DateTime dob = dateTimePicker1.Value;
DateTime currentDate = DateTime.Now;

int years = currentDate.Year - dob.Year;


int months = currentDate.Month - dob.Month;

if (currentDate.Day < dob.Day)


{
months--;
}

if (months < 0)
{
years--;
months += 12;
}

textBox1.Text = $"{years} years and {months} months";


}
}
}
Screen Shot:

Question No. 7 Design the following form using numericUpDown Control, button,
lable
Define the Maximum and Minimum Limit of the two numericupdown controls and
then display the time in AM/PM format, as an example is shown in above figure
Source Code:
namespace Question7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void buttonShowTime_Click(object sender, EventArgs e)


{
// get the minutes and seconds
int minutes, seconds;
String format = "";

minutes = Convert.ToInt16(numMin.Value);
seconds = Convert.ToInt16(numSec.Value);

// according to minues set format


if (minutes > 12)
{
format = "PM";
minutes = minutes - 12; // subtract 12 from minutes to get pm minutes
}
else
{
format = "AM";
}

// show time
if (minutes < 10 && seconds < 10)
{
// add 0 before minutes and seconds in time
MessageBox.Show("0" + minutes + ":" + "0" + seconds + " " + format);
}
else if (minutes < 10)
{
// add 0 before minutes in time
MessageBox.Show("0" + minutes + ":" + seconds + " " + format);
}
else if (seconds < 10)
{
// add 0 before seconds in time
MessageBox.Show(minutes + ":" + "0" + seconds + " " + format);
}
else
{
MessageBox.Show(minutes + ":" + seconds + " " + format);
}
}
}
}

Output:
Question No. 8 Design the following form using MaskText Control, button, lable

When you press the OK button, then the entered IP address will be shown in
MessageBox

Source Code:
namespace Question8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void buttonOk_Click(object sender, EventArgs e)


{
// get the entered ip address
String ipAddress = maskedTextBox1.Text.Replace(" ", ""); // replace placeholders with
nothing

// display entered ip address


MessageBox.Show("IP Address: " + ipAddress);
}
}
}

Output:

Question No. 9 Design the following form using RadioButtons, CheckBox,


GroupBox Control, button
When you click the Selected Movies button, then all of your selected Movie types
should be displayed in MessageBox
&
When you click the Favorite Movies button, then your Favorite Movie type should
be displayed in MessageBox

Source Code:
namespace Question9
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button2_Click(object sender, EventArgs e)


{
// var
String selectedMovies = "";
int sNo = 1;

// concat and show selected movie(s) at the end


if (cbComedy.Checked)
{
selectedMovies = sNo++ + ". " + cbComedy.Text + "\n";
}
if (cbAction.Checked)
{
selectedMovies = selectedMovies + sNo++ + ". " + cbAction.Text + "\n";
}
if (cbScience.Checked)
{
selectedMovies = selectedMovies + sNo++ + ". " + cbScience.Text + "\n";
}
if (cbRomance.Checked)
{
selectedMovies = selectedMovies + sNo++ + ". " + cbRomance.Text + "\n";
}
if (cbAnimation.Checked)
{
selectedMovies = selectedMovies + sNo++ + ". " + cbAnimation.Text + "\n";
}

// show message in message box


MessageBox.Show("Selected movies:\n\n" + selectedMovies);
}

private void buttonFavouriteMovie_Click(object sender, EventArgs e)


{
String favouriteMovie = "";

// check the selected movie and show at the end


if (rbComedy.Checked)
{
favouriteMovie = rbComedy.Text;
}
else if (rbAction.Checked)
{
favouriteMovie = rbAction.Text;
}
else if (rbScience.Checked)
{
favouriteMovie = rbScience.Text;
}
else if (rbRomance.Checked)
{
favouriteMovie = rbRomance.Text;
}
else if (rbAnimation.Checked)
{
favouriteMovie = rbAnimation.Text;
}

// message box
MessageBox.Show("Your favourite movie: " + favouriteMovie);
}
}
}

Output:

You might also like