/// <summary> /// Deletes the family if no article is linked to it /// </summary> /// <param name="Family"></param> private void Delete_Family(Models.Family Family) { DialogResult Res = MessageBox.Show(this, "Etes vous sûr de vouloir supprimer la famille : " + Family.Name + " ?", "Supprimer", MessageBoxButtons.YesNo, MessageBoxIcon.Information); // Todo detect if connected to an article // Only load the affected row if (Res == DialogResult.Yes) { // Todo detect if connected to an article // Only load the affected row Database db = Database.GetInstance(); if (db.Family_Has_Articles_Associated(Family.Id)) { Res = MessageBox.Show(this, "Erreur la famille est lié à un article", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } bool Sucess = db.Delete_Familly(Family.Id); if (Sucess) { Res = MessageBox.Show(this, "Suppression réussie !", "Succès", MessageBoxButtons.OK, MessageBoxIcon.Information); Family_List_View.Items.Remove(Family_List_View.SelectedItems[0]); } else { Res = MessageBox.Show(this, "Erreur lors de la suppression", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error); } } }
/// <summary> /// Displays the add family form /// </summary> private void Add_Family() { AddFamilyForm Form = new AddFamilyForm(null); DialogResult Result = Form.ShowDialog(); if (Result == DialogResult.OK) { // Get the new value. Database db = Database.GetInstance(); Models.Family Modified_Family = db.Get_Family_With_Id(Form.Inserted_Id); String[] row = { "" + Modified_Family.Id, Modified_Family.Name }; ListViewItem lvi = new ListViewItem(row); Family_List_View.Items.Add(lvi); } }
/// <summary> /// Displays the modify family form /// </summary> /// <param name="Family"></param> private void Modify_Family(Models.Family Family) { AddFamilyForm Form = new AddFamilyForm(Family.Name); DialogResult Result = Form.ShowDialog(); if (Result == DialogResult.OK) { // Get the new value. Database db = Database.GetInstance(); Models.Family Modified_Family = db.Get_Family_With_Id(Family.Id); ListViewItem Lvi = Family_List_View.SelectedItems[0]; Lvi.SubItems[0].Text = Modified_Family.Id.ToString(); Lvi.SubItems[1].Text = Modified_Family.Name; } }
/// <summary> /// Returns the currently selected item or null if none /// </summary> /// <returns>the currently selected item or null if none</returns> private Models.Family Get_Selected_Item() { if (this.Family_List_View.SelectedItems.Count != 0) { Models.Family Family = new Models.Family(); ListViewItem Item = this.Family_List_View.SelectedItems[0]; Family.Id = int.Parse(Item.SubItems[0].Text); Family.Name = Item.SubItems[1].Text; return(Family); } else { return(null); } }
/// <summary> /// Returns the list of all the families /// </summary> /// <returns>list of all the families</returns> public List <Models.Family> Get_Families() { List <Models.Family> Famillies = new List <Models.Family>(); System.Data.SQLite.SQLiteCommand cmd = SQL_Connection.CreateCommand(); cmd.CommandText = "SELECT * FROM Familles"; System.Data.SQLite.SQLiteDataReader Famillies_Reader = cmd.ExecuteReader(); while (Famillies_Reader.Read()) { Models.Family f = new Models.Family(); f.Id = Famillies_Reader.GetInt32(0); f.Name = Famillies_Reader.GetString(1); Famillies.Add(f); } return(Famillies); }
/// <summary> /// Returns the family from its id /// </summary> /// <param name="Id"></param> /// <returns>the family</returns> public Models.Family Get_Family_With_Id(int Id) { Models.Family Familly = new Models.Family(); System.Data.SQLite.SQLiteCommand cmd = SQL_Connection.CreateCommand(); cmd.CommandText = "SELECT * FROM Familles WHERE RefFamille = ?"; System.Data.SQLite.SQLiteParameter Id_Parameter = new System.Data.SQLite.SQLiteParameter(); Id_Parameter.Value = Id; cmd.Parameters.Add(Id_Parameter); System.Data.SQLite.SQLiteDataReader Familly_Reader = cmd.ExecuteReader(); if (Familly_Reader.Read()) { Familly.Id = Familly_Reader.GetInt32(0); Familly.Name = Familly_Reader.GetString(1); } return(Familly); }
/// <summary> /// Event handler for the F5, Enter & delete keys. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void On_Key_Pressed(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F5) { Load_Families(); } else if (e.KeyCode == Keys.Enter) { Models.Family Family = Get_Selected_Item(); if (Family != null) { Modify_Family(Family); } } else if (e.KeyCode == Keys.Delete) { Models.Family Family = Get_Selected_Item(); if (Family != null) { Delete_Family(Family); } } }