Imports ApplicationCrèche_1.IDFormMode Public Class FormParentID Inherits FormID ' ----- Private Attributes ----- Private actualFormMode As IDFormMode ' ----- Initialisators ----- Private Sub FormParentID_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load End Sub Public Sub New(theFormMode As IDFormMode, Optional aParent As Parent = Nothing) Console.WriteLine("FormParentID : Begining of a new instance") ' Cet appel est requis par le concepteur. InitializeComponent() ' Ajoutez une initialisation quelconque après l'appel InitializeComponent(). If theFormMode = CREATOR Or aParent Is Nothing Then ChangeMode(CREATOR) Else ChangeMode(theFormMode, aParent) End If End Sub ' ----- Methodes ----- Private Function getParentFromIntern() As Parent Dim newParent = New Parent(Me.Tag) newParent.firstname = TBFirstName.Text newParent.lastname = TBLastName.Text newParent.address = TBAddress.Text If (RBMale.Checked) Then newParent.gender = True Else newParent.gender = False End If newParent.phone = TBPhone.Text Return newParent End Function Private Function updateParentFromExtern(aParent As Parent) As Boolean If Not aParent Is Nothing Then ' Fill up controls with all the data we have. Me.Tag = aParent.id TBFirstName.Text = aParent.firstname 'Firstname TBLastName.Text = aParent.lastname 'Lastname If aParent.gender Then 'Gender RBMale.Checked = True Else RBFemale.Checked = True End If TBPhone.Text = aParent.phone TBAddress.Text = aParent.address FillListView(aParent.id) If ListView.Items.Count > 1 Then Label5.Text = "Children" Else Label5.Text = "Child" If (actualFormMode = VIEWER) Then Text = aParent.firstname + " " + aParent.lastname Else Text = aParent.firstname + " " + aParent.lastname + " (Edit)" End If Return True End If Return False End Function Private Sub ChangeMode(aFormMode As IDFormMode, Optional aParent As Parent = Nothing) If aFormMode = IDFormMode.CREATOR Then ' CREATOR MODE actualFormMode = CREATOR Console.WriteLine("FormParentID : Switch to CREATOR Mode") 'Change of the main triggers labels + title Text = "New parent" ButtonClose.Text = "Cancel" ButtonEdit.Text = "Save" RBMale.Checked = True ListView.Enabled = False Label5.Text = "" Me.Tag = 0 ElseIf aFormMode = IDFormMode.VIEWER And Not aParent Is Nothing Then ' VIEW MODE actualFormMode = VIEWER Console.WriteLine("FormParentID : Switch to VIEWER Mode") 'We change the main triggers labels ButtonClose.Text = "Close" ButtonEdit.Text = "Edit" Text = TBFirstName.Text + " " + TBLastName.Text + " (Edit)" 'Activate most important security aspect (not show a window that can mat a save somewhere else) TBAddress.Enabled = False TBFirstName.Enabled = False TBLastName.Enabled = False RBFemale.Enabled = False RBMale.Enabled = False TBPhone.Enabled = False ButtonPick.Enabled = False ListView.Enabled = False updateParentFromExtern(aParent) ElseIf aFormMode = IDFormMode.EDITOR Then ' EDITOR MODE Console.WriteLine("FormParentID : Switch to Editor Mode") actualFormMode = EDITOR ''We change the main triggers labels ButtonClose.Text = "Cancel" ButtonEdit.Text = "Update" 'Turn off anti edit security TBAddress.Enabled = True TBFirstName.Enabled = True TBLastName.Enabled = True RBFemale.Enabled = True RBMale.Enabled = True TBPhone.Enabled = True ButtonPick.Enabled = True ListView.Enabled = True updateParentFromExtern(aParent) Else Console.WriteLine("FormParentID : Return to Standart Mode !!") End If End Sub ''' ''' Get the list of children of the actual Parent. ''' ''' The id of the Parent ''' Private Sub FillListView(id As Integer) Dim theChildren As List(Of Child) = dbdata.getChildenOf(id) For Each aChild As Child In theChildren ListView.Items.Add(aChild.firstname + " " + aChild.lastname).Tag = aChild.id Next ' Child or Children ? If ListView.Items.Count <= 1 Then Label5.Text = "Child" Else Label5.Text = "Children" End If End Sub 'Edit and click Protected Overrides Sub ButtonEdit_Click(sender As System.Object, e As System.EventArgs) Dim Validated As Boolean = True If actualFormMode = VIEWER Then 'EDIT' ChangeMode(EDITOR) ElseIf actualFormMode = CREATOR Then 'SAVE' ' Error Provider If String.IsNullOrEmpty(TBFirstName.Text) Then ErrorProvider1.SetError(TBFirstName, "The first name field could not be empty." & vbNewLine & "Please fill this field.") Validated = False End If If String.IsNullOrEmpty(TBLastName.Text) Then ErrorProvider1.SetError(TBLastName, "The last name field could not be empty." & vbNewLine & "Please fill this field.") Validated = False End If If String.IsNullOrEmpty(TBPhone.Text) Then ErrorProvider1.SetError(TBPhone, "The phone number field could not be empty" & vbNewLine & "Please insert a correct phone number") Validated = False End If 'If TBPhone. = 0 Then ' ErrorProvider1.SetError(DTPBord, "The date of born could not be today" & vbNewLine & "Please insert a born date in the past") ' Validated = False 'End If If String.IsNullOrEmpty(TBAddress.Text) Then ErrorProvider1.SetError(LabelAddress, "The address field could not be empty." & vbNewLine & "Please fill this field.") Validated = False End If If Not Validated Then Exit Sub End If ' Validation Me.DialogResult = Windows.Forms.DialogResult.OK Me.Tag = dbdata.addParent(Me.getParentFromIntern()) Hide() ElseIf actualFormMode = EDITOR Then 'UPDATE' ' Error Provider If String.IsNullOrEmpty(TBFirstName.Text) Then ErrorProvider1.SetError(TBFirstName, "The first name field could not be empty." & vbNewLine & "Please fill this field.") Validated = False End If If String.IsNullOrEmpty(TBLastName.Text) Then ErrorProvider1.SetError(TBLastName, "The last name field could not be empty." & vbNewLine & "Please fill this field.") Validated = False End If If String.IsNullOrEmpty(TBPhone.Text) Then ErrorProvider1.SetError(TBPhone, "The phone number field could not be empty" & vbNewLine & "Please insert a correct phone number") Validated = False End If 'If TBPhone. = 0 Then ' ErrorProvider1.SetError(DTPBord, "The date of born could not be today" & vbNewLine & "Please insert a born date in the past") ' Validated = False 'End If If String.IsNullOrEmpty(TBAddress.Text) Then ErrorProvider1.SetError(LabelAddress, "The address field could not be empty." & vbNewLine & "Please fill this field.") Validated = False End If If Not Validated Then Exit Sub End If ' Validation Me.DialogResult = Windows.Forms.DialogResult.OK dbdata.editParent(Me.getParentFromIntern()) Hide() Else 'Don't know how to go here... Console.WriteLine("FormParentID : Is in standart mode !") Hide() End If End Sub Protected Overrides Sub ButtonClose_Click(sender As System.Object, e As System.EventArgs) Console.WriteLine("FormParentID : Closing instance") Me.DialogResult = Windows.Forms.DialogResult.Cancel Hide() End Sub Private Sub ButtonPick_Click(sender As System.Object, e As System.EventArgs) Handles ButtonPick.Click Dim FormListParent As FormList = New FormList(PersonType.Parent, ListFormMode.SelectWithAdress) Dim RD As DialogResult = FormListParent.ShowDialog If RD = Windows.Forms.DialogResult.OK Then Dim aParent As Parent = dbdata.getParentByID(FormListParent.Tag) TBAddress.Text = aParent.address End If End Sub End Class