Imports ApplicationCrèche_1.IDFormMode Public Class FormChildID Inherits FormID ' ----- Private Attributes ----- Private actualFormMode As IDFormMode Private meetID As Integer Private oldBSID As Integer Private newBSID As Integer ' ----- Initialisators ----- Private Sub FormChildID_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load End Sub Public Sub New(aFormMode As IDFormMode, Optional aChild As Child = Nothing) Console.WriteLine("FormChildID : Begining of a new instance") ' Cet appel est requis par le concepteur. InitializeComponent() ' Ajoutez une initialisation quelconque après l'appel InitializeComponent(). If aFormMode = CREATOR Or aChild Is Nothing Then ChangeMode(CREATOR) oldBSID = 0 meetID = 0 Else ChangeMode(aFormMode, aChild) If Not aChild.meet Is Nothing Then oldBSID = dbdata.getBabysitterByMeetID(aChild.meet.id).id End If End If End Sub ' ----- Methods ------ Private Function refreshBS(aBS As BabySitter) If Not aBS Is Nothing Then Me.LabelBS.Text = aBS.firstname + " " + aBS.lastname + " | " + aBS.phone1 newBSID = aBS.id Return True Else LabelBS.Text = "" newBSID = Nothing End If Return False End Function Public Function refreshParent1(aParent As Parent) As Boolean If Not aParent Is Nothing Then Me.LabelParent1.Text = aParent.firstname + " " + aParent.lastname + " | " + aParent.phone Me.LabelParent1.Tag = aParent.id Return True Else Me.LabelParent2.Tag = 0 Me.LabelParent2.Text = "subscrib a new parent" End If Return False End Function Public Function refreshParent2(aParent As Parent) As Boolean If Not aParent Is Nothing Then Me.LabelParent2.Text = aParent.firstname + " " + aParent.lastname + " | " + aParent.phone Me.LabelParent2.Tag = aParent.id Return True Else Me.LabelParent2.Tag = 0 Me.LabelParent2.Text = "subscrib a new parent" End If Return False End Function Private Sub setupCheckboxes(aMeet As Meet) CBFriday.Checked = aMeet.friday CBMonday.Checked = aMeet.monday CBWednesday.Checked = aMeet.wednesday CBSaturday.Checked = aMeet.saturday CBSunday.Checked = aMeet.sunday CBThursday.Checked = aMeet.thursday CBTuesday.Checked = aMeet.tuesday meetID = aMeet.id End Sub ''' ''' Get the meet from the form. If the meet is loaded from the database, his ID is added. Else the meet have an id = to 0 ''' ''' The current meet ''' The meet number is based on the old recorded BabySitter ID (oldBSID) Private Function getMeet() As Meet Dim aMeet As Meet = Nothing If oldBSID = 0 Then aMeet = New Meet(0) Else aMeet = New Meet(CInt(meetID)) End If aMeet.monday = CBMonday.Checked aMeet.tuesday = CBTuesday.Checked aMeet.wednesday = CBWednesday.Checked aMeet.thursday = CBThursday.Checked aMeet.friday = CBFriday.Checked aMeet.saturday = CBSaturday.Checked aMeet.sunday = CBSunday.Checked Return aMeet End Function ''' ''' Get the Child with all informations that the user have filled up. ''' ''' a Child with the ID 0 if it is a new child or this current ID. ''' Public Function getChildFromIntern() As Child Dim newChild As Child = New Child(Me.Tag) newChild.firstname = TBFirstName.Text newChild.lastname = TBLastName.Text newChild.gender = RBMale.Checked newChild.address = TBAddress.Text newChild.bornDate = DTPBord.Value If Not LabelParent1.Tag Is Nothing Then newChild.parent1 = dbdata.getParentByID(LabelParent1.Tag) End If If Not LabelParent2.Tag Is Nothing Then newChild.parent2 = dbdata.getParentByID(LabelParent2.Tag) End If newChild.meet = getMeet() Return (newChild) End Function ''' ''' Fill all controls with information given as a Child ''' ''' The child to load ''' false is the parameter is nothing else true. ''' Private Function updateChildFromExtern(aChild As Child) As Boolean ' Fill up controls with all the data we have. If Not aChild Is Nothing Then Me.Tag = aChild.id 'Me.Text = aChild.firstname + " " + aChild.lastname Me.TBFirstName.Text = aChild.firstname 'Firstname Me.TBLastName.Text = aChild.lastname 'Lastname If aChild.gender Then 'Gender Me.RBMale.Checked = True Else Me.RBFemale.Checked = True End If Me.DTPBord.Value = aChild.bornDate 'BornDate Me.LabelAge.Text = DateDiff(DateInterval.Year, aChild.bornDate, Date.Now).ToString + " year(s) old" 'Age Me.TBAddress.Text = aChild.address 'Address If Not aChild.parent1 Is Nothing Then 'Parent1 Info Me.LabelParent1.Text = aChild.parent1.firstname + " " + aChild.parent1.lastname + " | " + aChild.parent1.phone Me.LabelParent1.Tag = aChild.parent1.id Else Me.LabelParent1.Text = "subscrib a new parent" End If If Not aChild.parent2 Is Nothing Then 'Parent2 Info Me.LabelParent2.Text = aChild.parent2.firstname + " " + aChild.parent2.lastname + " | " + aChild.parent2.phone Me.LabelParent2.Tag = aChild.parent2.id Else Me.LabelParent2.Text = "subscrib a new parent" End If 'Title If actualFormMode = VIEWER Then Me.Text = aChild.firstname + " " + aChild.lastname Else Me.Text = aChild.firstname + " " + aChild.lastname + " (Edit)" End If ' A babysitter is loader only if the child have a meet If Not aChild.meet Is Nothing Then Dim aBS As BabySitter = dbdata.getBabysitterByMeetID(aChild.meet.id) refreshBS(aBS) 'The babysitter id is stored with data setupCheckboxes(aChild.meet) 'The meet id is stored with data End If Return True End If Return False End Function ''' ''' Polyvalence Mechanism. Change the 'MODE' of the windows ''' ''' The new mode like EDITOR or CREATOR ''' Some new info about the child. ''' Private Sub ChangeMode(aFormMode As IDFormMode, Optional aChild As Child = Nothing) If aFormMode = IDFormMode.CREATOR Then ' CREATOR MODE Me.actualFormMode = CREATOR Console.WriteLine("FormChildID : Switch to CREATOR Mode") 'Change of the main triggers labels + title 'Me.Icon Me.Text = "New child" Me.ButtonClose.Text = "Cancel" Me.ButtonEdit.Text = "Save" Me.RBMale.Checked = True Me.LabelAge.Text = "" Me.ButtonEdit.Image = My.Resources.AddSheet2 Me.Tag = 0 ElseIf aFormMode = IDFormMode.VIEWER And Not aChild Is Nothing Then ' VIEW MODE Me.actualFormMode = VIEWER Console.WriteLine("FormChildID : Switch to VIEWER Mode") 'We change the main triggers labels + title Me.ButtonClose.Text = "Close" Me.ButtonEdit.Text = "Edit" Me.ButtonEdit.Image = My.Resources.EditSheet2 'Activate most important security aspect (not show a window that can mat a save somewhere else) ButtonParent1.Enabled = False ButtonParent2.Enabled = False ButtonBS.Enabled = False LabelParent1.Enabled = False LabelParent2.Enabled = False TBFirstName.Enabled = False TBLastName.Enabled = False DTPBord.Enabled = False RBFemale.Enabled = False RBMale.Enabled = False TBAddress.Enabled = False CBFriday.Enabled = False CBMonday.Enabled = False CBSaturday.Enabled = False CBSunday.Enabled = False CBThursday.Enabled = False CBTuesday.Enabled = False CBWednesday.Enabled = False LabelBS.Enabled = False ButtonBS.Enabled = False ElseIf aFormMode = IDFormMode.EDITOR Then ' EDITOR MODE WITH THE CURRENT CHILD Console.WriteLine("FormChildID : Switch to Editor Mode") Me.actualFormMode = EDITOR ''We change the main triggers labels + title Me.ButtonClose.Text = "Cancel" Me.ButtonEdit.Text = "Update" Me.ButtonEdit.Image = My.Resources.ValidedSheet Me.Text = TBFirstName.Text + " " + TBLastName.Text + " (Edit)" 'Turn off anti edit security ButtonParent1.Enabled = True ButtonParent2.Enabled = True ButtonBS.Enabled = True LabelParent1.Enabled = True LabelParent2.Enabled = True TBFirstName.Enabled = True TBLastName.Enabled = True DTPBord.Enabled = True RBFemale.Enabled = True RBMale.Enabled = True TBAddress.Enabled = True CBFriday.Enabled = True CBMonday.Enabled = True CBSaturday.Enabled = True CBSunday.Enabled = True CBThursday.Enabled = True CBTuesday.Enabled = True CBWednesday.Enabled = True LabelBS.Enabled = True ButtonBS.Enabled = True Else Console.WriteLine("FormChildId : No correct mode has been found.") End If ' Fill up controls with all the data we have. updateChildFromExtern(aChild) End Sub ' ----- Handles and Listerners ----- 'Edit and click Protected Overrides Sub ButtonEdit_Click(sender As System.Object, e As System.EventArgs) Dim Validated As Boolean = True If Me.actualFormMode = VIEWER Then 'EDIT' Me.ChangeMode(EDITOR) ElseIf Me.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 Date.Compare(DTPBord.Value, Today) > 0 Then ErrorProvider1.SetError(DTPBord, "The date of born is in the futur." & vbNewLine & "Please insert a valid born date.") Validated = False End If 'If Date.Compare(Today, DTPBord.Value) = 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 (CBMonday.Checked = True Or CBTuesday.Checked = True Or CBWednesday.Checked = True Or CBThursday.Checked = True Or CBFriday.Checked = True Or CBSunday.Checked = True Or CBSunday.Checked = True) And newBSID = 0 Then ErrorProvider1.SetError(ButtonBS, "There is no babysitter for the current planning" & vbNewLine & "Please select a baysitter on the list or uncheck all the days.") Validated = False End If If Not Validated Then Exit Sub End If ' Record Dim aChild As Child = getChildFromIntern() ' We save the child on the database Me.Tag = dbdata.addChild(aChild) ' The new ID of the child is stored in the Me.tag If newBSID <> 0 Then ' save of the child meetID = dbdata.addMeet(aChild.meet, Me.Tag) ' then update of the babysitter dbdata.updateBabysitterMeets(meetID, newBSID, getFirstFreeMeetOf(newBSID)) End If Me.DialogResult = Windows.Forms.DialogResult.OK Me.Hide() ElseIf Me.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 Date.Compare(DTPBord.Value, Today) > 0 Then ErrorProvider1.SetError(DTPBord, "The date of born is in the futur." & vbNewLine & "Please insert a valid born date.") Validated = False End If 'If Date.Compare(Today, DTPBord.Value) = 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 (CBMonday.Checked = True Or CBTuesday.Checked = True Or CBWednesday.Checked = True Or CBThursday.Checked = True Or CBFriday.Checked = True Or CBSunday.Checked = True Or CBSunday.Checked = True) And newBSID = 0 Then ErrorProvider1.SetError(ButtonBS, "There is no babysitter for the current planning" & vbNewLine & "Please select a baysitter on the list or uncheck all the days.") Validated = False End If If Not Validated Then Exit Sub End If ' Record Dim aChild As Child = getChildFromIntern() dbdata.editChild(aChild) 'Update of the child data 'Case 1, there is no choosen BS If newBSID <> oldBSID And newBSID = 0 Then dbdata.updateBabysitterMeets(0, , , oldBSID, getFirstFreeMeetOf(oldBSID)) 'The babysitter gain a free meet space dbdata.deleteMeet(meetID) ' No BS, no keep ElseIf newBSID = oldBSID And oldBSID <> 0 Then 'Case 2 : The same BS is here Dim aMeet As Meet = aChild.meet 'The meet could have change or not between the database and the form, no problem. The Id stay the same. dbdata.editMeet(aMeet) ' No neet to update the BS, because the meet ID stay the same. ElseIf newBSID <> oldBSID And newBSID <> 0 And oldBSID <> 0 Then ' Case 3 : There is a change between old BS and new BS Dim aMeet As Meet = aChild.meet 'The meet have change between the database and the form but the Id stay the same. dbdata.editMeet(aMeet) dbdata.updateBabysitterMeets(meetID, newBSID, getFirstFreeMeetOf(newBSID), oldBSID, getFirstFreeMeetOf(oldBSID)) 'So we need to remove the id from the old BS and add it to the new BS ElseIf newBSID <> 0 And oldBSID = 0 Then ' Case 4 : add a new BS on a existing child Dim aMeet As Meet = aChild.meet meetID = dbdata.addMeet(aMeet, aChild.id) Dim aBS As BabySitter = dbdata.getBabysitterByID(newBSID) dbdata.updateBabysitterMeets(meetID, newBSID, getFirstFreeMeetOf(newBSID)) End If Me.DialogResult = Windows.Forms.DialogResult.OK Me.Hide() Else 'Don't know how to go here... Console.WriteLine("FormChildID : Is in standart mode !") End If End Sub 'Cancel Protected Overrides Sub ButtonClose_Click(sender As System.Object, e As System.EventArgs) Console.WriteLine("FormChildID : Closing instance") Me.DialogResult = Windows.Forms.DialogResult.Cancel Me.Hide() End Sub ' Parents selector Private Sub ButtonParent1_Click(sender As System.Object, e As System.EventArgs) Handles ButtonParent1.Click Dim listofParentForm As FormList = New FormList(PersonType.Parent, ListFormMode.Selector) Dim conclusion As DialogResult = listofParentForm.ShowDialog() Console.WriteLine("Returned in : " + conclusion.ToString) If (conclusion = Windows.Forms.DialogResult.OK) Then Dim aParent As Parent = dbdata.getParentByID(listofParentForm.Tag) Console.WriteLine("FormChildID : receving " + aParent.toString) refreshParent1(aParent) Else If dbdata.getParentByID(listofParentForm.Tag) Is Nothing Then refreshParent2(Nothing) End If End If listofParentForm.Dispose() End Sub Private Sub LabelParent1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LabelParent1.LinkClicked Dim aParentForm As FormParentID If Me.LabelParent1.Tag <> 0 Then Dim theParent As Parent = dbdata.getParentByID(Me.LabelParent1.Tag) aParentForm = New FormParentID(EDITOR, theParent) Else aParentForm = New FormParentID(CREATOR) End If Dim conclusion As DialogResult = aParentForm.ShowDialog() If (conclusion = Windows.Forms.DialogResult.OK) Then Dim aParent As Parent = dbdata.getParentByID(aParentForm.Tag) Console.WriteLine("FormChildID : receving " + aParent.toString) refreshParent1(aParent) End If aParentForm.Dispose() End Sub Private Sub ButtonParent2_Click(sender As System.Object, e As System.EventArgs) Handles ButtonParent2.Click Dim listofParentForm As FormList = New FormList(PersonType.Parent, ListFormMode.Selector) Dim conclusion As DialogResult = listofParentForm.ShowDialog() Console.WriteLine("Returned in : " + conclusion.ToString) If (conclusion = Windows.Forms.DialogResult.OK) Then Dim aParent As Parent = dbdata.getParentByID(listofParentForm.Tag) Console.WriteLine("FormChildID : receving " + aParent.toString) refreshParent2(aParent) Else If dbdata.getParentByID(listofParentForm.Tag) Is Nothing Then refreshParent2(Nothing) End If End If listofParentForm.Dispose() End Sub Private Sub LabelParent2_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LabelParent2.LinkClicked Dim aParentForm As FormParentID If Me.LabelParent1.Tag <> 0 Then Dim theParent As Parent = dbdata.getParentByID(Me.LabelParent2.Tag) aParentForm = New FormParentID(EDITOR, theParent) Else aParentForm = New FormParentID(CREATOR) End If Dim conclusion As DialogResult = aParentForm.ShowDialog() Console.WriteLine("Returned in : " + conclusion.ToString) If (conclusion = Windows.Forms.DialogResult.OK) Then Dim aParent As Parent = dbdata.getParentByID(aParentForm.Tag) Console.WriteLine("FormChildID : receving " + aParent.toString) refreshParent2(aParent) End If aParentForm.Dispose() End Sub Private Sub ButtonBS_Click(sender As System.Object, e As System.EventArgs) Handles ButtonBS.Click Dim aMeet As Meet = getMeet() Dim listBD As FormList = New FormList(PersonType.BabySitter, ListFormMode.SelectFiltered, aMeet) Dim conclusion As DialogResult = listBD.ShowDialog() If conclusion = Windows.Forms.DialogResult.OK Then Dim aBS As BabySitter = dbdata.getBabysitterByID(listBD.Tag) Console.WriteLine("FormChildID : receving " + aBS.toString) refreshBS(aBS) End If listBD.Dispose() End Sub Private Sub LinkLabelBS_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LabelBS.LinkClicked If newBSID <> 0 Then Dim aBS As BabySitter = dbdata.getBabysitterByID(newBSID) Dim bsForm As FormBabySitterID = New FormBabySitterID(VIEWER, aBS) Dim conclusion As DialogResult = bsForm.ShowDialog() End If End Sub Private Sub DTPBord_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DTPBord.ValueChanged Me.LabelAge.Text = DateDiff(DateInterval.Year, DTPBord.Value, Date.Now).ToString + " year(s) old" 'Age ErrorProvider1.SetError(DTPBord, "") End Sub Private Sub CBWednesday_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CBMonday.CheckedChanged, CBTuesday.CheckedChanged, CBWednesday.CheckedChanged, CBThursday.CheckedChanged, CBFriday.CheckedChanged, CBSaturday.CheckedChanged, CBSunday.CheckedChanged If Me.Visible = True Then 'On réintialise la babysitter refreshBS(Nothing) End If ErrorProvider1.SetError(ButtonBS, "") End Sub End Class