I started out with the best of intentions, but this form got hacky real fast.
It's purpose is to serve as a create new Student form. Also, if you want to view an existing Students information.
Think of it as the CRU of CRUD.
Here it is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tutomentor.Branding;
using Tutomentor.Data.Repositories;
namespace Tutomentor.UI.Students
{
public partial class StudentInformation : Form
{
StudentRepository repo = new StudentRepository();
bool IsCreating = false;
Student student;
public StudentInformation()
{
InitializeComponent();
LoadComboBoxes();
LoadBranding();
IsCreating = true;
}
public StudentInformation(int studentID)
{
InitializeComponent();
LoadComboBoxes();
LoadBranding();
student = repo.FindStudent(studentID);
LoadStudentInformation(student);
}
private void LoadComboBoxes()
{
cmbGrade.DisplayMember = "Name";
cmbGrade.ValueMember = "ID";
cmbGradeParalelo.DisplayMember = "Name";
cmbGradeParalelo.ValueMember = "ID";
GradeRepository repo = new GradeRepository();
cmbGrade.DataSource = repo.FindAllGrades();
}
private void LoadStudentInformation(Student student)
{
cmbGrade.SelectedValue = student.GradeParalelo.Grade.ID;
cmbGradeParalelo.SelectedValue = student.IDGrade;
txtRude.Text = student.RUDE.ToString();
txtNombrePadre.Text = student.FatherName;
txtProfesionPadre.Text = student.FatherProfession;
txtCelularPadre.Text = student.MobilePhoneFather;
txtLugarDeTrabajoPadre.Text = student.PlaceofWorkFather;
txtNombreMadre.Text = student.MotherName;
txtProfesionMadre.Text = student.MotherProfession;
txtCelularMadre.Text = student.MobilePhoneMother;
txtLugarDeTrabajoMadre.Text = student.PlaceofWorkMother;
txtObservaciones.Text = student.Observations;
txtNombre.Text = student.Name;
txtApellidoPaterno.Text = student.FatherLastName;
txtApellidoMaterno.Text = student.MotherLasteName;
dtpFechaNacimiento.Value = Convert.ToDateTime(student.DateOfBirth);
txtLugarNacimiento.Text = student.PlaceOfBirth;
SetSex(student.Sex);
txtCarnet.Text = student.Carnet;
txtTelefono.Text = student.Telephone;
txtCelular.Text = student.MobilePhone;
txtDireccion.Text = student.Address;
}
private void SetSex(string p)
{
if (p == "M")
{
sexoMasculino.Checked = true;
sexoFemenino.Checked = false;
}
else
{
sexoMasculino.Checked = false;
sexoFemenino.Checked = true;
}
}
private void LoadBranding()
{
this.Text = "Tutomentor - Agregando nuevo alumnos.";
this.BackColor = Brand.PrimaryColor;
panelBorderLeft.BackColor = Brand.HeaderColor;
panelBorderRight.BackColor = Brand.HeaderColor;
panelSeparator1.BackColor = Brand.SeparatorColor;
}
private void cmbGrade_SelectedIndexChanged(object sender, EventArgs e)
{
LoadGradeParalelos();
}
private void LoadGradeParalelos()
{
GradeParaleloRepository repo = new GradeParaleloRepository();
Int64 gradeID = Convert.ToInt64(cmbGrade.SelectedValue);
cmbGradeParalelo.DataSource = repo.FindAllGradeParalelos().Where(g => g.IDGrade == gradeID);
}
private void btnSave_Click(object sender, EventArgs e)
{
SaveInformation();
}
private void SaveInformation()
{
if (IsCreating)
{
Student newStudent = new Student();
Int64 gradeId = Convert.ToInt64(cmbGradeParalelo.SelectedValue);
newStudent.IDGrade = gradeId;
newStudent.RUDE = Convert.ToInt64(txtRude.Text);
/*Parents information.*/
newStudent.FatherName = txtNombrePadre.Text;
newStudent.FatherProfession = txtProfesionPadre.Text;
newStudent.MobilePhoneFather = FormatPhoneNumber(txtCelularPadre.Text);
newStudent.PlaceofWorkFather = txtLugarDeTrabajoPadre.Text;
newStudent.MotherName = txtNombreMadre.Text;
newStudent.MotherProfession = txtProfesionMadre.Text;
newStudent.MobilePhoneMother = FormatPhoneNumber(txtCelularMadre.Text);
newStudent.PlaceofWorkMother = txtLugarDeTrabajoMadre.Text;
/*newStudent information*/
newStudent.Name = txtNombre.Text;
newStudent.FatherLastName = txtApellidoPaterno.Text;
newStudent.MotherLasteName = txtApellidoMaterno.Text;
newStudent.DateOfBirth = dtpFechaNacimiento.Value.ToShortDateString();
newStudent.PlaceOfBirth = txtLugarNacimiento.Text;
newStudent.Sex = sexoMasculino.Checked ? sexoMasculino.Text : sexoFemenino.Text;
newStudent.Telephone = FormatPhoneNumber(txtTelefono.Text);
newStudent.MobilePhone = FormatPhoneNumber(txtCelular.Text);
newStudent.Address = txtDireccion.Text;
newStudent.Carnet = FormatPhoneNumber(txtCarnet.Text);
newStudent.Observations = txtObservaciones.Text;
repo.Add(newStudent);
repo.Save();
MessageBox.Show("Se guardo el registro exitosamente.",
"Exito!",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
ClearForm();
}
else
{
Int64 gradeId = Convert.ToInt64(cmbGradeParalelo.SelectedValue);
student.IDGrade = gradeId;
student.RUDE = Convert.ToInt64(txtRude.Text);
/*Parents information.*/
student.FatherName = txtNombrePadre.Text;
student.FatherProfession = txtProfesionPadre.Text;
student.MobilePhoneFather = FormatPhoneNumber(txtCelularPadre.Text);
student.PlaceofWorkFather = txtLugarDeTrabajoPadre.Text;
student.MotherName = txtNombreMadre.Text;
student.MotherProfession = txtProfesionMadre.Text;
student.MobilePhoneMother = FormatPhoneNumber(txtCelularMadre.Text);
student.PlaceofWorkMother = txtLugarDeTrabajoMadre.Text;
/*student information*/
student.Name = txtNombre.Text;
student.FatherLastName = txtApellidoPaterno.Text;
student.MotherLasteName = txtApellidoMaterno.Text;
student.DateOfBirth = dtpFechaNacimiento.Value.ToShortDateString();
student.PlaceOfBirth = txtLugarNacimiento.Text;
student.Sex = sexoMasculino.Checked ? sexoMasculino.Text : sexoFemenino.Text;
student.Telephone = FormatPhoneNumber(txtTelefono.Text);
student.MobilePhone = FormatPhoneNumber(txtCelular.Text);
student.Address = txtDireccion.Text;
student.Carnet = FormatPhoneNumber(txtCarnet.Text);
student.Observations = txtObservaciones.Text;
repo.Save();
MessageBox.Show("Se guardo el registro exitosamente.",
"Exito!",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
this.Close();
}
}
private string FormatPhoneNumber(string p)
{
return p.Insert(3, "-");
}
private void btnClear_Click(object sender, EventArgs e)
{
ClearForm();
}
private void ClearForm()
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
}
}
}
I don't know how to improve this class because every line of code is needed.