using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; namespace Fuel01 { public partial class f_vehi : Form { DataSet1.tb_vehiDataTable tb_vehi = new DataSet1.tb_vehiDataTable(); private bool hasChanged = false; List lproduit = new List(); List lcarbu = new List(); List lfamille = new List(); public f_vehi() { InitializeComponent(); dg_vehi.AutoGenerateColumns = false; dg_vehi.DataSource = tbvehiculeBindingSource; } private void vehi_Load(object sender, EventArgs e) { Program.subfolder = Program.folder + @"\" + Program.key_ope; if (!Directory.Exists(Program.subfolder)) Directory.CreateDirectory(Program.subfolder); else if ( File.Exists(Program.subfolder + @"\vehicule.json")) tb_vehi = DbUtil.LoadFromJson(Program.subfolder + @"\vehicule.json", tb_vehi); tb_vehi.AcceptChanges(); tbvehiculeBindingSource.DataSource = tb_vehi; turn_txt(false); turn_bt(false); if (Program.key_ope != null) this.Text = "Véhicules " + Program.nom_ope; if (File.Exists(Program.folder + @"\param\famille.xml")) init_list_famille(); if (File.Exists(Program.folder + @"\param\produit.xml")) init_list_produit(); } private void init_list_famille() { XDocument xRoot = XDocument.Load(Program.folder + @"\param\famille.xml"); var data = from item in xRoot.Descendants("tb_famille") orderby item.Element("key_fam").Value select new famille() { key_fam = item.Element("key_fam").Value }; lfamille = data.ToList(); combo_famille.Items.Clear(); combo_famille.DisplayMember = "key_fam"; foreach (famille f in lfamille) combo_famille.Items.Add(f); } private void init_list_produit() { XDocument xRoot = XDocument.Load(Program.folder + @"\param\produit.xml"); var data = from item in xRoot.Descendants("tb_prod") orderby item.Element("type_prod").Value, item.Element("lib_prod").Value select new produit() { key_prod = item.Element("key_prod").Value, lib_prod = item.Element("lib_prod").Value, type_prod = item.Element("type_prod").Value }; lproduit = data.ToList(); lcarbu=lproduit.Where(p => p.type_prod == "Carburant").ToList(); combo_energy.Items.Clear(); combo_energy.DisplayMember = "key_prod"; foreach (produit p in lcarbu) combo_energy.Items.Add(p); } #region affichage private void turn_txt(bool act) { text_immat_vehi.Enabled = act; text_num_vehi.Enabled = act; text_cmpnum_vehi.Enabled = act; combo_categ_vehi.Enabled = act; text_type_vehi.Enabled = act; text_kmdep_vehi.Enabled = act; text_kmarr_vehi.Enabled = act; text_cond_vehi.Enabled = act; combo_energy.Enabled = act; combo_famille.Enabled = act; } private void turn_bt(bool act) { Bt_Abort.Enabled = act; bt_Valid.Enabled = act; bt_Mod.Enabled = !act; bt_Aj.Enabled = !act; bt_Sup.Enabled = !act; dg_vehi.Enabled = !act; } #endregion #region Boutons private void bt_Mod_Click(object sender, EventArgs e) { maj_sort_vehi(); turn_bt(true); turn_txt(true); } private void bt_Aj_Click(object sender, EventArgs e) { turn_txt(true); turn_bt(true); string key = findNewkey(); DataSet1.tb_vehiRow myNewRow=tb_vehi.Newtb_vehiRow(); myNewRow["key_vehi"] = key; myNewRow["ope_vehi"] = Program.key_ope; tb_vehi.Addtb_vehiRow(myNewRow); tbvehiculeBindingSource.MoveFirst(); DataRowView myRow = tbvehiculeBindingSource.Current as DataRowView; while (myRow.Row["key_vehi"].ToString() != key) { tbvehiculeBindingSource.MoveNext(); myRow = tbvehiculeBindingSource.Current as DataRowView; } } private void bt_Sup_Click(object sender, EventArgs e) { DataRowView myRow = tbvehiculeBindingSource.Current as DataRowView; if (myRow == null) return; string msg = string.Format("Vehicule {0}\nEtes-vous certain de vouloir supprimer \n{1}", myRow.Row["num_vehi"].ToString(), myRow.Row["immat_vehi"].ToString()); string caption = "Suppression définitive"; if (MessageBox.Show(msg, caption, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes) { DataSet1.tb_vehiRow vehiRow = tb_vehi.FindBykey_vehi(myRow.Row["key_vehi"].ToString()); if (vehiRow != null) tb_vehi.Removetb_vehiRow(vehiRow); hasChanged = true; } } private void bt_Valid_Click(object sender, EventArgs e) { maj_sort_vehi(); tb_vehi.AcceptChanges(); turn_txt(false); turn_bt(false); hasChanged = true; } private void Bt_Abort_Click(object sender, EventArgs e) { tb_vehi.RejectChanges(); turn_txt(false); turn_bt(false); } #endregion #region fonctions private string findNewkey() { Random rand1 = new Random(); string key = rand1.Next(99999).ToString(); while (tb_vehi.FindBykey_vehi(key) != null) { MessageBox.Show(key); key = rand1.Next(99999).ToString(); } return key; } #endregion private void f_vehi_FormClosing(object sender, FormClosingEventArgs e) { if (hasChanged) saveData(); DataTable tb_vehi_change = tb_vehi.GetChanges(); tb_vehi_change = null; if (tb_vehi_change != null) MessageBox.Show("Veuillez Valider ou Abandonner", "Modifications en cours"); e.Cancel = (tb_vehi_change != null); } private void saveData() { if (File.Exists(Program.subfolder + @"\vehicule.json")) File.Copy(Program.subfolder + @"\vehicule.json", Program.subfolder + @"\vehicule.back.json",true); tb_vehi = DbUtil.LoadFromJson(Program.subfolder + @"\vehicule.json", tb_vehi); } private void combo_energy_TextUpdate(object sender, EventArgs e) { produit cur_Prod = lproduit.Find(p => p.key_prod == combo_energy.Text); if (cur_Prod != null) StatusLabel1.Text = cur_Prod.lib_prod; else StatusLabel1.Text = "..."; } private void maj_sort_vehi() { DataRowView myRow = tbvehiculeBindingSource.Current as DataRowView; if ( myRow!=null) myRow["sort1_vehi"]= string.Format("{0}{1:00000}{2}", myRow["categ_vehi"], myRow["num_vehi"], myRow["cmpnum_vehi"]); } private void text_num_vehi_Leave(object sender, EventArgs e) { maj_sort_vehi(); } private void label2_Click(object sender, EventArgs e) { } private void text_immat_vehi_TextChanged(object sender, EventArgs e) { } } }