0% found this document useful (0 votes)
9 views28 pages

New Rich Text Document

The document outlines a C# Windows Forms application for managing a school system, featuring a login form, main menu, and forms for managing students and teachers. It includes functionality for user authentication, displaying and adding students and teachers, and connecting to a SQL database. The application is structured with multiple forms and event handlers for user interactions.

Uploaded by

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

New Rich Text Document

The document outlines a C# Windows Forms application for managing a school system, featuring a login form, main menu, and forms for managing students and teachers. It includes functionality for user authentication, displaying and adding students and teachers, and connecting to a SQL database. The application is structured with multiple forms and event handlers for user interactions.

Uploaded by

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

using System;

using System.Data.SqlClient;

using System.Windows.Forms;

namespace ScolariteApp

static class Program

[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(new LoginForm());

public partial class LoginForm : Form

private TextBox txtUsername;

private TextBox txtPassword;

private Button btnLogin;

private Label label1;

private Label label2;

public LoginForm()
{

InitializeComponent();

private void InitializeComponent()

this.txtUsername = new System.Windows.Forms.TextBox();

this.txtPassword = new System.Windows.Forms.TextBox();

this.btnLogin = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// txtUsername

//

this.txtUsername.Location = new System.Drawing.Point(120, 50);

this.txtUsername.Name = "txtUsername";

this.txtUsername.Size = new System.Drawing.Size(150, 20);

this.txtUsername.TabIndex = 0;

//

// txtPassword

//

this.txtPassword.Location = new System.Drawing.Point(120, 90);

this.txtPassword.Name = "txtPassword";

this.txtPassword.PasswordChar = '*';

this.txtPassword.Size = new System.Drawing.Size(150, 20);


this.txtPassword.TabIndex = 1;

//

// btnLogin

//

this.btnLogin.Location = new System.Drawing.Point(150, 130);

this.btnLogin.Name = "btnLogin";

this.btnLogin.Size = new System.Drawing.Size(75, 23);

this.btnLogin.TabIndex = 2;

this.btnLogin.Text = "Login";

this.btnLogin.UseVisualStyleBackColor = true;

this.btnLogin.Click += new System.EventHandler(this.btnLogin_Click);

//

// label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(50, 53);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(55, 13);

this.label1.TabIndex = 3;

this.label1.Text = "Username";

//

// label2

//

this.label2.AutoSize = true;

this.label2.Location = new System.Drawing.Point(50, 93);

this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(53, 13);

this.label2.TabIndex = 4;

this.label2.Text = "Password";

//

// LoginForm

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(350, 200);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Controls.Add(this.btnLogin);

this.Controls.Add(this.txtPassword);

this.Controls.Add(this.txtUsername);

this.Name = "LoginForm";

this.Text = "Login";

this.ResumeLayout(false);

this.PerformLayout();

private void btnLogin_Click(object sender, EventArgs e)

string username = txtUsername.Text;

string password = txtPassword.Text;

// ‫تحقق بسيط من اسم المستخدم وكلمة المرور‬


if (username == "admin" && password == "admin")

MessageBox.Show("Login successful!");

MainForm mainForm = new MainForm();

mainForm.Show();

this.Hide();

else

MessageBox.Show("Invalid username or password.");

public partial class MainForm : Form

private Button btnManageStudents;

private Button btnManageTeachers;

private Button btnManageSubjects;

private Button btnManageResults;

private Button btnManageAttendance;

public MainForm()

InitializeComponent();

}
private void InitializeComponent()

this.btnManageStudents = new System.Windows.Forms.Button();

this.btnManageTeachers = new System.Windows.Forms.Button();

this.btnManageSubjects = new System.Windows.Forms.Button();

this.btnManageResults = new System.Windows.Forms.Button();

this.btnManageAttendance = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// btnManageStudents

//

this.btnManageStudents.Location = new System.Drawing.Point(50, 30);

this.btnManageStudents.Name = "btnManageStudents";

this.btnManageStudents.Size = new System.Drawing.Size(150, 40);

this.btnManageStudents.TabIndex = 0;

this.btnManageStudents.Text = "Manage Students";

this.btnManageStudents.UseVisualStyleBackColor = true;

this.btnManageStudents.Click += new
System.EventHandler(this.btnManageStudents_Click);

//

// btnManageTeachers

//

this.btnManageTeachers.Location = new System.Drawing.Point(50, 90);

this.btnManageTeachers.Name = "btnManageTeachers";

this.btnManageTeachers.Size = new System.Drawing.Size(150, 40);

this.btnManageTeachers.TabIndex = 1;
this.btnManageTeachers.Text = "Manage Teachers";

this.btnManageTeachers.UseVisualStyleBackColor = true;

this.btnManageTeachers.Click += new
System.EventHandler(this.btnManageTeachers_Click);

//

// btnManageSubjects

//

this.btnManageSubjects.Location = new System.Drawing.Point(50, 150);

this.btnManageSubjects.Name = "btnManageSubjects";

this.btnManageSubjects.Size = new System.Drawing.Size(150, 40);

this.btnManageSubjects.TabIndex = 2;

this.btnManageSubjects.Text = "Manage Subjects";

this.btnManageSubjects.UseVisualStyleBackColor = true;

this.btnManageSubjects.Click += new
System.EventHandler(this.btnManageSubjects_Click);

//

// btnManageResults

//

this.btnManageResults.Location = new System.Drawing.Point(50, 210);

this.btnManageResults.Name = "btnManageResults";

this.btnManageResults.Size = new System.Drawing.Size(150, 40);

this.btnManageResults.TabIndex = 3;

this.btnManageResults.Text = "Manage Results";

this.btnManageResults.UseVisualStyleBackColor = true;

this.btnManageResults.Click += new
System.EventHandler(this.btnManageResults_Click);

//

// btnManageAttendance
//

this.btnManageAttendance.Location = new System.Drawing.Point(50, 270);

this.btnManageAttendance.Name = "btnManageAttendance";

this.btnManageAttendance.Size = new System.Drawing.Size(150, 40);

this.btnManageAttendance.TabIndex = 4;

this.btnManageAttendance.Text = "Manage Attendance";

this.btnManageAttendance.UseVisualStyleBackColor = true;

this.btnManageAttendance.Click += new
System.EventHandler(this.btnManageAttendance_Click);

//

// MainForm

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(250, 350);

this.Controls.Add(this.btnManageAttendance);

this.Controls.Add(this.btnManageResults);

this.Controls.Add(this.btnManageSubjects);

this.Controls.Add(this.btnManageTeachers);

this.Controls.Add(this.btnManageStudents);

this.Name = "MainForm";

this.Text = "Main Menu";

this.ResumeLayout(false);

private void btnManageStudents_Click(object sender, EventArgs e)

{
StudentsForm studentsForm = new StudentsForm();

studentsForm.Show();

private void btnManageTeachers_Click(object sender, EventArgs e)

TeachersForm teachersForm = new TeachersForm();

teachersForm.Show();

private void btnManageSubjects_Click(object sender, EventArgs e)

SubjectsForm subjectsForm = new SubjectsForm();

subjectsForm.Show();

private void btnManageResults_Click(object sender, EventArgs e)

ResultsForm resultsForm = new ResultsForm();

resultsForm.Show();

private void btnManageAttendance_Click(object sender, EventArgs e)

AttendanceForm attendanceForm = new AttendanceForm();

attendanceForm.Show();
}

public partial class StudentsForm : Form

private ListBox listBoxStudents;

private TextBox txtName;

private TextBox txtGrade;

private Button btnAddStudent;

private Label label1;

private Label label2;

private string connectionString = "Server=.;Database=ScolariteDB;Integrated


Security=True;";

private SqlConnection connection;

public StudentsForm()

InitializeComponent();

connection = new SqlConnection(connectionString);

LoadStudents();

private void InitializeComponent()

this.listBoxStudents = new System.Windows.Forms.ListBox();

this.txtName = new System.Windows.Forms.TextBox();


this.txtGrade = new System.Windows.Forms.TextBox();

this.btnAddStudent = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// listBoxStudents

//

this.listBoxStudents.FormattingEnabled = true;

this.listBoxStudents.Location = new System.Drawing.Point(20, 20);

this.listBoxStudents.Name = "listBoxStudents";

this.listBoxStudents.Size = new System.Drawing.Size(300, 160);

this.listBoxStudents.TabIndex = 0;

//

// txtName

//

this.txtName.Location = new System.Drawing.Point(120, 200);

this.txtName.Name = "txtName";

this.txtName.Size = new System.Drawing.Size(150, 20);

this.txtName.TabIndex = 1;

//

// txtGrade

//

this.txtGrade.Location = new System.Drawing.Point(120, 230);

this.txtGrade.Name = "txtGrade";

this.txtGrade.Size = new System.Drawing.Size(150, 20);


this.txtGrade.TabIndex = 2;

//

// btnAddStudent

//

this.btnAddStudent.Location = new System.Drawing.Point(150, 260);

this.btnAddStudent.Name = "btnAddStudent";

this.btnAddStudent.Size = new System.Drawing.Size(100, 30);

this.btnAddStudent.TabIndex = 3;

this.btnAddStudent.Text = "Add Student";

this.btnAddStudent.UseVisualStyleBackColor = true;

this.btnAddStudent.Click += new System.EventHandler(this.btnAddStudent_Click);

//

// label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(50, 203);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(35, 13);

this.label1.TabIndex = 4;

this.label1.Text = "Name";

//

// label2

//

this.label2.AutoSize = true;

this.label2.Location = new System.Drawing.Point(50, 233);

this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(36, 13);

this.label2.TabIndex = 5;

this.label2.Text = "Grade";

//

// StudentsForm

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(350, 320);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Controls.Add(this.btnAddStudent);

this.Controls.Add(this.txtGrade);

this.Controls.Add(this.txtName);

this.Controls.Add(this.listBoxStudents);

this.Name = "StudentsForm";

this.Text = "Manage Students";

this.ResumeLayout(false);

this.PerformLayout();

private void LoadStudents()

try

connection.Open();
string query = "SELECT * FROM Students";

SqlCommand command = new SqlCommand(query, connection);

SqlDataReader reader = command.ExecuteReader();

listBoxStudents.Items.Clear();

while (reader.Read())

string studentInfo = $"ID: {reader["StudentID"]}, Name: {reader["Name"]}, Grade:


{reader["Grade"]}";

listBoxStudents.Items.Add(studentInfo);

reader.Close();

catch (Exception ex)

MessageBox.Show("Error: " + ex.Message);

finally

connection.Close();

private void btnAddStudent_Click(object sender, EventArgs e)

string name = txtName.Text;

string grade = txtGrade.Text;


if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(grade))

MessageBox.Show("Please enter name and grade.");

return;

try

connection.Open();

string query = "INSERT INTO Students (Name, Grade) VALUES (@Name, @Grade)";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@Name", name);

command.Parameters.AddWithValue("@Grade", grade);

command.ExecuteNonQuery();

MessageBox.Show("Student added successfully!");

txtName.Clear();

txtGrade.Clear();

LoadStudents();

catch (Exception ex)

MessageBox.Show("Error: " + ex.Message);

finally
{

connection.Close();

public partial class TeachersForm : Form

private ListBox listBoxTeachers;

private TextBox txtName;

private TextBox txtSpecialization;

private Button btnAddTeacher;

private Label label1;

private Label label2;

private string connectionString = "Server=.;Database=ScolariteDB;Integrated


Security=True;";

private SqlConnection connection;

public TeachersForm()

InitializeComponent();

connection = new SqlConnection(connectionString);

LoadTeachers();

private void InitializeComponent()


{

this.listBoxTeachers = new System.Windows.Forms.ListBox();

this.txtName = new System.Windows.Forms.TextBox();

this.txtSpecialization = new System.Windows.Forms.TextBox();

this.btnAddTeacher = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// listBoxTeachers

//

this.listBoxTeachers.FormattingEnabled = true;

this.listBoxTeachers.Location = new System.Drawing.Point(20, 20);

this.listBoxTeachers.Name = "listBoxTeachers";

this.listBoxTeachers.Size = new System.Drawing.Size(300, 160);

this.listBoxTeachers.TabIndex = 0;

//

// txtName

//

this.txtName.Location = new System.Drawing.Point(120, 200);

this.txtName.Name = "txtName";

this.txtName.Size = new System.Drawing.Size(150, 20);

this.txtName.TabIndex = 1;

//

// txtSpecialization

//
this.txtSpecialization.Location = new System.Drawing.Point(120, 230);

this.txtSpecialization.Name = "txtSpecialization";

this.txtSpecialization.Size = new System.Drawing.Size(150, 20);

this.txtSpecialization.TabIndex = 2;

//

// btnAddTeacher

//

this.btnAddTeacher.Location = new System.Drawing.Point(150, 260);

this.btnAddTeacher.Name = "btnAddTeacher";

this.btnAddTeacher.Size = new System.Drawing.Size(100, 30);

this.btnAddTeacher.TabIndex = 3;

this.btnAddTeacher.Text = "Add Teacher";

this.btnAddTeacher.UseVisualStyleBackColor = true;

this.btnAddTeacher.Click += new System.EventHandler(this.btnAddTeacher_Click);

//

// label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(50, 203);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(35, 13);

this.label1.TabIndex = 4;

this.label1.Text = "Name";

//

// label2

//
this.label2.AutoSize = true;

this.label2.Location = new System.Drawing.Point(50, 233);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(73, 13);

this.label2.TabIndex = 5;

this.label2.Text = "Specialization";

//

// TeachersForm

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(350, 320);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Controls.Add(this.btnAddTeacher);

this.Controls.Add(this.txtSpecialization);

this.Controls.Add(this.txtName);

this.Controls.Add(this.listBoxTeachers);

this.Name = "TeachersForm";

this.Text = "Manage Teachers";

this.ResumeLayout(false);

this.PerformLayout();

private void LoadTeachers()

{
try

connection.Open();

string query = "SELECT * FROM Teachers";

SqlCommand command = new SqlCommand(query, connection);

SqlDataReader reader = command.ExecuteReader();

listBoxTeachers.Items.Clear();

while (reader.Read())

string teacherInfo = $"ID: {reader["TeacherID"]}, Name: {reader["Name"]},


Specialization: {reader["Specialization"]}";

listBoxTeachers.Items.Add(teacherInfo);

reader.Close();

catch (Exception ex)

MessageBox.Show("Error: " + ex.Message);

finally

connection.Close();

private void btnAddTeacher_Click(object sender, EventArgs e)


{

string name = txtName.Text;

string specialization = txtSpecialization.Text;

if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(specialization))

MessageBox.Show("Please enter name and specialization.");

return;

try

connection.Open();

string query = "INSERT INTO Teachers (Name, Specialization) VALUES (@Name,


@Specialization)";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@Name", name);

command.Parameters.AddWithValue("@Specialization", specialization);

command.ExecuteNonQuery();

MessageBox.Show("Teacher added successfully!");

txtName.Clear();

txtSpecialization.Clear();

LoadTeachers();

catch (Exception ex)

{
MessageBox.Show("Error: " + ex.Message);

finally

connection.Close();

public partial class SubjectsForm : Form

private ListBox listBoxSubjects;

private TextBox txtName;

private TextBox txtTeacherID;

private Button btnAddSubject;

private Label label1;

private Label label2;

private string connectionString = "Server=.;Database=ScolariteDB;Integrated


Security=True;";

private SqlConnection connection;

public SubjectsForm()

InitializeComponent();

connection = new SqlConnection(connectionString);

LoadSubjects();
}

private void InitializeComponent()

this.listBoxSubjects = new System.Windows.Forms.ListBox();

this.txtName = new System.Windows.Forms.TextBox();

this.txtTeacherID = new System.Windows.Forms.TextBox();

this.btnAddSubject = new System.Windows.Forms.Button();

this.label1 = new System.Windows.Forms.Label();

this.label2 = new System.Windows.Forms.Label();

this.SuspendLayout();

//

// listBoxSubjects

//

this.listBoxSubjects.FormattingEnabled = true;

this.listBoxSubjects.Location = new System.Drawing.Point(20, 20);

this.listBoxSubjects.Name = "listBoxSubjects";

this.listBoxSubjects.Size = new System.Drawing.Size(300, 160);

this.listBoxSubjects.TabIndex = 0;

//

// txtName

//

this.txtName.Location = new System.Drawing.Point(120, 200);

this.txtName.Name = "txtName";

this.txtName.Size = new System.Drawing.Size(150, 20);

this.txtName.TabIndex = 1;
//

// txtTeacherID

//

this.txtTeacherID.Location = new System.Drawing.Point(120, 230);

this.txtTeacherID.Name = "txtTeacherID";

this.txtTeacherID.Size = new System.Drawing.Size(150, 20);

this.txtTeacherID.TabIndex = 2;

//

// btnAddSubject

//

this.btnAddSubject.Location = new System.Drawing.Point(150, 260);

this.btnAddSubject.Name = "btnAddSubject";

this.btnAddSubject.Size = new System.Drawing.Size(100, 30);

this.btnAddSubject.TabIndex = 3;

this.btnAddSubject.Text = "Add Subject";

this.btnAddSubject.UseVisualStyleBackColor = true;

this.btnAddSubject.Click += new System.EventHandler(this.btnAddSubject_Click);

//

// label1

//

this.label1.AutoSize = true;

this.label1.Location = new System.Drawing.Point(50, 203);

this.label1.Name = "label1";

this.label1.Size = new System.Drawing.Size(35, 13);

this.label1.TabIndex = 4;

this.label1.Text = "Name";
//

// label2

//

this.label2.AutoSize = true;

this.label2.Location = new System.Drawing.Point(50, 233);

this.label2.Name = "label2";

this.label2.Size = new System.Drawing.Size(60, 13);

this.label2.TabIndex = 5;

this.label2.Text = "Teacher ID";

//

// SubjectsForm

//

this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

this.ClientSize = new System.Drawing.Size(350, 320);

this.Controls.Add(this.label2);

this.Controls.Add(this.label1);

this.Controls.Add(this.btnAddSubject);

this.Controls.Add(this.txtTeacherID);

this.Controls.Add(this.txtName);

this.Controls.Add(this.listBoxSubjects);

this.Name = "SubjectsForm";

this.Text = "Manage Subjects";

this.ResumeLayout(false);

this.PerformLayout();

}
private void LoadSubjects()

try

connection.Open();

string query = "SELECT * FROM Subjects";

SqlCommand command = new SqlCommand(query, connection);

SqlDataReader reader = command.ExecuteReader();

listBoxSubjects.Items.Clear();

while (reader.Read())

string subjectInfo = $"ID: {reader["SubjectID"]}, Name: {reader["Name"]}, TeacherID:


{reader["TeacherID"]}";

listBoxSubjects.Items.Add(subjectInfo);

reader.Close();

catch (Exception ex)

MessageBox.Show("Error: " + ex.Message);

finally

connection.Close();

}
}

private void btnAddSubject_Click(object sender, EventArgs e)

string name = txtName.Text;

int teacherID = int.Parse(txtTeacherID.Text);

if (string.IsNullOrEmpty(name) || teacherID <= 0)

MessageBox.Show("Please enter subject name and teacher ID.");

return;

try

connection.Open();

string query = "INSERT INTO Subjects (Name, TeacherID) VALUES (@Name,


@TeacherID)";

SqlCommand command = new SqlCommand(query, connection);

command.Parameters.AddWithValue("@Name", name);

command.Parameters.AddWithValue("@TeacherID", teacherID);

command.ExecuteNonQuery();

MessageBox.Show("Subject added successfully!");

txtName.Clear();

txtTeacherID.Clear();

LoadSubjects();
}

catch (Exception ex)

MessageBox.Show("Error: " + ex.Message);

finally

connection.Close();

You might also like