using DatenDB; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Deckungsbeitrag.AA_Forms { public partial class FormNeuerBenutzer : Form { private Fehlermeldungen meldung = new Fehlermeldungen(); bool[] tosave = { false, false, false }; public FormNeuerBenutzer() { InitializeComponent(); } private void FormNeuerBenutzer_Load(object sender, EventArgs e) { this.comboBoxRolle.DataSource = Enum.GetValues(typeof(BenutzerRolle)); this.comboBoxRolle.SelectedIndex = -1; this.dateTimePickerFSGueltig.Value = DateTime.Today; } private void buttonSpeichern_Click(object sender, EventArgs e) { Benutzer ben = new Benutzer(); ben.Aktiv = true; ben.Vorname = textBoxVorname.Text; ben.Nachname = textBoxNachname.Text; ben.BenutzerName = textBoxBenutzername.Text; ben.Rolle = (BenutzerRolle)comboBoxRolle.SelectedValue; if(ben.Rolle == BenutzerRolle.Fahrer) { ben.Schein = int.Parse(textBoxFuehrerschein.Text); ben.GueltigBis = dateTimePickerFSGueltig.Value; } ben.BenutzerID = null; if (ben.Save() != 1) meldung.Speicherfehler(); else { string s = $"Benutzer {ben.BenutzerName} erfolgreich gespeichert."; meldung.SpeichernErfolgreich(s); this.DialogResult = DialogResult.OK; this.Close(); } } private void buttonAbbrechen_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; this.Close(); } /// /// Wenn SelectedValueChanged wird auch tosave geändert. Validierung wird auch hier ausgelöst. /// /// /// private void comboBoxRolle_SelectedValueChanged(object sender, EventArgs e) { if (this.comboBoxRolle.SelectedValue != null) { if ((BenutzerRolle)this.comboBoxRolle.SelectedValue == BenutzerRolle.Fahrer) { this.Height = 450; this.groupBoxFuehrerschein.Enabled = this.groupBoxFuehrerschein.Visible = true; this.textBoxFuehrerschein.Focus(); tosave[2] = false; } else tosave[2] = true; CancelEventArgs ce = new CancelEventArgs(); comboBoxRolle_Validating(comboBoxRolle, ce); } else tosave[2] = false; } /// /// Speicherbuttonstatus Regelung tosave werdem geprüft. /// private void Enable_Speichern() { if (!Array.TrueForAll(tosave, b => b == true)) { this.buttonSpeichern.Enabled = false; this.buttonSpeichern.BackColor = Color.Gray; } else { this.buttonSpeichern.Enabled = true; this.buttonSpeichern.BackColor = Color.FromArgb(0, 192, 0); this.buttonSpeichern.Focus(); } } /// /// TextBox Benutzername validieren und Speicherbuttonstatus regeln. /// /// /// private void textBoxBenutzername_Validating(object sender, CancelEventArgs e) { if (textBoxBenutzername.Text == string.Empty) { string s = $"Der Benutzername ist verpflichtend."; meldung.EingabeFehltoderFalsch(s); e.Cancel = true; textBoxBenutzername.Focus(); textBoxBenutzername.BackColor = Color.Red; } else textBoxBenutzername.BackColor = SystemColors.Window; tosave[0] = !e.Cancel; Enable_Speichern(); } /// /// ComboBox BenutzerRolle validieren und Speicherbuttonstatus regeln. /// /// /// private void comboBoxRolle_Validating(object sender, CancelEventArgs e) { if (string.IsNullOrWhiteSpace(comboBoxRolle.Text)) { string s = $"Die BenutzerRolle muss ausgewählt sein."; meldung.EingabeFehltoderFalsch(s); e.Cancel = true; comboBoxRolle.Focus(); comboBoxRolle.BackColor = Color.Red; } else { comboBoxRolle.BackColor = SystemColors.Window; if ((BenutzerRolle)comboBoxRolle.SelectedValue == BenutzerRolle.Fahrer) tosave[2] = false; else tosave[2] = true; } tosave[1] = !e.Cancel; Enable_Speichern(); } /// /// GroupBox Führerschein validieren wenn sichtbar. Speicherbuttonstatus regeln wenn sichtbar. /// /// /// private void groupBoxFuehrerschein_Validating(object sender, CancelEventArgs e) { if (string.IsNullOrWhiteSpace(textBoxFuehrerschein.Text) || dateTimePickerFSGueltig.Value == DateTime.Today) { string s = $"Wenn Benutzerrolle ist Fahrer, müssen Führerscheinnummer und Ablaufdatum ausgefüllt werden.{Environment.NewLine}Möchtest du die Benutzerrolle wechseln?"; if (meldung.BenutzerRolleWechseln(s) == DialogResult.No) { e.Cancel = true; groupBoxFuehrerschein.ForeColor = Color.Red; tosave[2] = !e.Cancel; } else { groupBoxFuehrerschein.Visible = false; this.Height = 300; tosave[2] = e.Cancel; } } else { groupBoxFuehrerschein.ForeColor = Color.White; tosave[2] = !e.Cancel; } Enable_Speichern(); } /// /// TextBox Führerschein validieren. Keine Regelung für Speicherbuttonstatus. /// /// /// private void textBoxFuehrerschein_Validating(object sender, CancelEventArgs e) { if (!string.IsNullOrWhiteSpace(textBoxFuehrerschein.Text)) { if (int.TryParse(textBoxFuehrerschein.Text, out int value)) textBoxFuehrerschein.BackColor = SystemColors.Window; else { meldung.Eingabefehler(); e.Cancel = true; textBoxFuehrerschein.BackColor = Color.Red; } } else textBoxFuehrerschein.BackColor = SystemColors.Window; } /// /// Leave-Events zur unterstützung der Speicherbuttonstatus Regelung. /// /// /// private void dateTimePickerFSGueltig_Leave(object sender, EventArgs e) { CancelEventArgs ce = new CancelEventArgs(); groupBoxFuehrerschein_Validating(groupBoxFuehrerschein, ce); } private void comboBoxRolle_Leave(object sender, EventArgs e) { CancelEventArgs ce = new CancelEventArgs(); comboBoxRolle_Validating(comboBoxRolle, ce); } private void textBoxFuehrerschein_Leave(object sender, EventArgs e) { CancelEventArgs ce = new CancelEventArgs(); textBoxFuehrerschein_Validating(textBoxFuehrerschein, ce); } /// /// Leave-Events ohne Auswirkung auf Speicherbuttonstatus Regelung. /// /// /// private void textBoxVorname_Leave(object sender, EventArgs e) { this.textBoxBenutzername.Text = textBoxVorname.Text; } private void textBoxNachname_Leave(object sender, EventArgs e) { this.textBoxBenutzername.Text += textBoxNachname.Text; } /// /// ComboBox Hintergrundfarbe verändern wenn DropDown geöffnet wird. /// /// /// private void comboBoxRolle_DropDown(object sender, EventArgs e) { comboBoxRolle.BackColor = SystemColors.Window; } } }