0% found this document useful (0 votes)
11 views12 pages

Custodio Mac Paul A. Assignment 3

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

Custodio Mac Paul A. Assignment 3

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

Mac Paul A.

Custodio Application Development and Emerging Technologies


BSIT 3-2N

Assignment #3:

- The Application is created using WPF App (.NET Framework)

- Create a separate C# file in the project for a class named ‘Student.cs’ for student
record:
namespace StudentRecordsApp.Models
{
public class Student
{
public int StudentID { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public string Level { get; set; }
public string Section { get; set; }
}
}

- XAML File for the Main Window UI named ‘MainWindow.xaml’:


<Window x:Class="StudentRecordsApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
mc:Ignorable="d"
Title="STUDENT RECORD MANAGEMENT APP" Height="450" Width="800">
<Grid Background="{DynamicResource {x:Static
SystemColors.ActiveCaptionBrushKey}}">
<DataGrid x:Name="StudentDataGrid" AutoGenerateColumns="False"
IsReadOnly="True" Margin="10,76,10,10" BorderBrush="#FF060606"
FontFamily="Bahnschrift Light Condensed" FontSize="16">
<DataGrid.Columns>
<DataGridTextColumn Header="Student ID" Binding="{Binding
StudentID}" Width="*"/>
<DataGridTextColumn Header="First Name" Binding="{Binding
FirstName}" Width="*"/>
<DataGridTextColumn Header="Middle Name" Binding="{Binding
MiddleName}" Width="*"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding
LastName}" Width="*"/>
<DataGridTextColumn Header="Level" Binding="{Binding Level}"
Width="*"/>
<DataGridTextColumn Header="Section" Binding="{Binding
Section}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"
VerticalAlignment="Bottom" Margin="10">
<Button Content="Add" Width="100" Margin="5"
Click="AddButton_Click" FontFamily="Bahnschrift Light Condensed"
FontSize="16" Background="{DynamicResource {x:Static
SystemColors.GradientActiveCaptionBrushKey}}"/>
<Button Content="Modify" Width="100" Margin="5"
Click="ModifyButton_Click" FontFamily="Bahnschrift Light Condensed"
FontSize="16" Background="{DynamicResource {x:Static
SystemColors.GradientActiveCaptionBrushKey}}"/>
<Button Content="Delete" Width="100" Margin="5"
Click="DeleteButton_Click" FontFamily="Bahnschrift Light Condensed"
FontSize="16" Background="{DynamicResource {x:Static
SystemColors.GradientActiveCaptionBrushKey}}"/>
</StackPanel>
<TextBlock HorizontalAlignment="Center" Margin="0,28,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="290" Foreground="Black"
Height="21" FontFamily="Bahnschrift Light Condensed" FontSize="20"><Run
Text="STUDENT RECORD MANAGEMENT APPLICATION"/><LineBreak/><Run/></TextBlock>
</Grid>
</Window>

- ‘MainWindow.xaml’ Design:
- ‘MainWindow.xaml.cs’ for the functionality of the Main Window:

using StudentRecordsApp.Models;
using System.Collections.ObjectModel;
using System.Windows;

namespace StudentRecordsApp
{
public partial class MainWindow : Window
{
public ObservableCollection<Student> Students { get; set; }

public MainWindow()
{
InitializeComponent();
Students = new ObservableCollection<Student>();
StudentDataGrid.ItemsSource = Students;
}

private void AddButton_Click(object sender, RoutedEventArgs e)


{
StudentWindow addStudentWindow = new StudentWindow();
if (addStudentWindow.ShowDialog() == true)
{
Students.Add(addStudentWindow.Student);
}
}

private void ModifyButton_Click(object sender, RoutedEventArgs e)


{
if (StudentDataGrid.SelectedItem is Student selectedStudent)
{
StudentWindow modifyStudentWindow = new
StudentWindow(selectedStudent);
if (modifyStudentWindow.ShowDialog() == true)
{
// Update student details
int index = Students.IndexOf(selectedStudent);
Students[index] = modifyStudentWindow.Student;
}
}
else
{
MessageBox.Show("Please select a record to modify.", "Modify
Student Record", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}

private void DeleteButton_Click(object sender, RoutedEventArgs e)


{
if (StudentDataGrid.SelectedItem is Student selectedStudent)
{
Students.Remove(selectedStudent);
}
else
{
MessageBox.Show("Please select a record to delete.", "Delete
Student Record", MessageBoxButton.OK, MessageBoxImage.Warning);
}
}
private void TextBox_TextChanged(object sender,
System.Windows.Controls.TextChangedEventArgs e)
{

}
}
}

- XAML File for the Add/Modify Student Window UI named ‘StudentWindow.xaml’:


<Window x:Class="StudentRecordsApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-
compatibility/2006"
mc:Ignorable="d"
Title="STUDENT RECORD MANAGEMENT APP" Height="450" Width="800">
<Grid Background="{DynamicResource {x:Static
SystemColors.ActiveCaptionBrushKey}}">
<DataGrid x:Name="StudentDataGrid" AutoGenerateColumns="False"
IsReadOnly="True" Margin="10,76,10,10" BorderBrush="#FF060606"
FontFamily="Bahnschrift Light Condensed" FontSize="16">
<DataGrid.Columns>
<DataGridTextColumn Header="Student ID" Binding="{Binding
StudentID}" Width="*"/>
<DataGridTextColumn Header="First Name" Binding="{Binding
FirstName}" Width="*"/>
<DataGridTextColumn Header="Middle Name" Binding="{Binding
MiddleName}" Width="*"/>
<DataGridTextColumn Header="Last Name" Binding="{Binding
LastName}" Width="*"/>
<DataGridTextColumn Header="Level" Binding="{Binding Level}"
Width="*"/>
<DataGridTextColumn Header="Section" Binding="{Binding
Section}" Width="*"/>
</DataGrid.Columns>
</DataGrid>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center"
VerticalAlignment="Bottom" Margin="10">
<Button Content="Add" Width="100" Margin="5"
Click="AddButton_Click" FontFamily="Bahnschrift Light Condensed"
FontSize="16" Background="{DynamicResource {x:Static
SystemColors.GradientActiveCaptionBrushKey}}"/>
<Button Content="Modify" Width="100" Margin="5"
Click="ModifyButton_Click" FontFamily="Bahnschrift Light Condensed"
FontSize="16" Background="{DynamicResource {x:Static
SystemColors.GradientActiveCaptionBrushKey}}"/>
<Button Content="Delete" Width="100" Margin="5"
Click="DeleteButton_Click" FontFamily="Bahnschrift Light Condensed"
FontSize="16" Background="{DynamicResource {x:Static
SystemColors.GradientActiveCaptionBrushKey}}"/>
</StackPanel>
<TextBlock HorizontalAlignment="Center" Margin="0,28,0,0"
TextWrapping="Wrap" VerticalAlignment="Top" Width="290" Foreground="Black"
Height="21" FontFamily="Bahnschrift Light Condensed" FontSize="20"><Run
Text="STUDENT RECORD MANAGEMENT APPLICATION"/><LineBreak/><Run/></TextBlock>
</Grid>
</Window>
- ‘StudentWindow.xaml’ Design:

- ‘StudentWindow.xaml.cs’ for the functionality of the Add/Modify Student Window:


using StudentRecordsApp.Models;
using System;
using System.Text.RegularExpressions;
using System.Windows;

namespace StudentRecordsApp
{
public partial class StudentWindow : Window
{
public Student Student { get; private set; }

public StudentWindow()
{
InitializeComponent();
}

public StudentWindow(Student student) : this()


{
txtStudentID.Text = student.StudentID.ToString();
txtFirstName.Text = student.FirstName;
txtMiddleName.Text = student.MiddleName;
txtLastName.Text = student.LastName;
txtLevel.Text = student.Level;
txtSection.Text = student.Section;
Student = student;
}

private void SaveButton_Click(object sender, RoutedEventArgs e)


{
if (ValidateInputs())
{
Student = new Student
{
StudentID = int.Parse(txtStudentID.Text),
FirstName = txtFirstName.Text,
MiddleName = txtMiddleName.Text,
LastName = txtLastName.Text,
Level = txtLevel.Text,
Section = txtSection.Text
};
DialogResult = true;
Close();
}
}

private void CancelButton_Click(object sender, RoutedEventArgs e)


{
DialogResult = false;
Close();
}

private bool ValidateInputs()


{
// Validate StudentID
if (!Regex.IsMatch(txtStudentID.Text, @"^\d+$"))
{
MessageBox.Show("Student ID should only be numbers.", "Input
Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}

// Validate FirstName, MiddleName, LastName


if (txtFirstName.Text.Length > 100)
{
MessageBox.Show("First Name can only accept up to 100
characters.", "Input Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
if (txtMiddleName.Text.Length > 100)
{
MessageBox.Show("Middle Name can only accept up to 100
characters.", "Input Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
if (txtLastName.Text.Length > 100)
{
MessageBox.Show("Last Name can only accept up to 100
characters.", "Input Error", MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}

// Validate Level and Section


if (string.IsNullOrWhiteSpace(txtLevel.Text))
{
MessageBox.Show("Level is required.", "Input Error",
MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
if (string.IsNullOrWhiteSpace(txtSection.Text))
{
MessageBox.Show("Section is required.", "Input Error",
MessageBoxButton.OK, MessageBoxImage.Error);
return false;
}
return true;
}

private void txtStudentID_TextChanged(object sender,


System.Windows.Controls.TextChangedEventArgs e)
{

private void txtFirstName_TextChanged(object sender,


System.Windows.Controls.TextChangedEventArgs e)
{

private void txtSection_TextChanged(object sender,


System.Windows.Controls.TextChangedEventArgs e)
{

private void TextBox_TextChanged(object sender,


System.Windows.Controls.TextChangedEventArgs e)
{

}
}
}

OUTPUT:

- Main Window UI upon starting:


- Add/Modify Student Window pops up when the ‘Add’ button is pressed:

- Input the following details then press ‘Save’:


- RESULT:

- Click the row you want to modify then press the ‘Modify’ Button to make the
Add/Modify Student Window pop up again then after modifying, click ‘Save’:
- Click the row you want to delete then press the ‘Delete’ button:

- VALIDATIONS:
a.) ID should only be numbers.
b.) First Name, Middle Name, and Last Name can only accept up to 100 characters in
length.
c.) Cannot be saved if there are no level and section tagged in the student.

You might also like