Advertisement
Guest User

VB Schedule Generator

a guest
Feb 12th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Runtime.InteropServices
  2.  
  3. Public Class Form1
  4.     Dim a As Boolean = False
  5.     Public doc As XDocument = XDocument.Load("ScheduleConfig.xml")
  6.     Public firstschedule As XDocument = New XDocument(New XElement("firstschedule"))
  7.     'Section 1: Adding Grade Levels
  8.    Private Sub GradeLevelsButton_Click(sender As Object, e As EventArgs) Handles GradeLevelsButton.Click 'When the Add button is clicked
  9.        Dim gl As XElement = New XElement("gradelevel") 'Declare the <gradelevel> tag
  10.        Dim pass As Boolean = True 'If the name for the grade level is valid, then true, or else false
  11.        For Each x As String In GradeLevelsBox.Items
  12.             If x.Equals(GradeLevelsTextBox.Text) Then 'Invalid grade level name: there's already one with the same name
  13.                MsgBox("There is already a grade with the same name.", MsgBoxStyle.Critical)
  14.                 pass = False
  15.             ElseIf x.Equals("") Then 'Invalid grade level name: it's empty
  16.                MsgBox("The grade name is empty.", MsgBoxStyle.Critical)
  17.                 pass = False
  18.             End If
  19.         Next
  20.         If pass = True Then 'If the name for the grade level is valid
  21.            gl.SetAttributeValue("name", GradeLevelsTextBox.Text) 'Add the name attribute to <gradelevel>
  22.            GradeLevelsBox.Items.Add(GradeLevelsTextBox.Text) 'Add the grade level to the listbox
  23.            doc.Root.Add(gl) 'Add <gradelevel> to the XDocument
  24.        End If
  25.     End Sub
  26.  
  27.     Private Sub GradeLevelsBox_Click() Handles GradeLevelsBox.Click 'When the GradeLevels listbox is clicked
  28.        If GradeLevelsBox.SelectedItem = Nothing Then 'If nothing is selected, disable everything in the grade level editor
  29.            MandatoryClassBox.Enabled = False
  30.             OptionalClassBox.Enabled = False
  31.             MandatoryTextBox.Enabled = False
  32.             OptionalTextBox.Enabled = False
  33.             MandatoryButton.Enabled = False
  34.             OptionalButton.Enabled = False
  35.             Button3.Enabled = False
  36.         Else 'If something is selected
  37.            MandatoryClassBox.Enabled = True 'Enable everything in the grade level editor
  38.            OptionalClassBox.Enabled = True
  39.             MandatoryTextBox.Enabled = True
  40.             OptionalTextBox.Enabled = True
  41.             MandatoryButton.Enabled = True
  42.             OptionalButton.Enabled = True
  43.             Button3.Enabled = True
  44.             GradeLevelEdit_Load()
  45.         End If
  46.     End Sub
  47.  
  48.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'When the program opens up
  49.        For Each a As XElement In doc.Root.Elements() 'Iterate through the XElements in the root element (<schedule>)
  50.            If a.Name = "gradelevel" Then 'If the <gradelevel> element is found
  51.                GradeLevelsBox.Items.Add(a.FirstAttribute.Value) 'Add the value of the name attribute to the grade level listbox
  52.            End If
  53.         Next
  54.         GroupBox3.Visible = True 'Position the following groupboxes to their respected positions and visibilities
  55.        GroupBox3.Location = New Point(460, 105)
  56.         GroupBox2.Visible = True
  57.         GroupBox2.Location = New Point(0, 137)
  58.         GroupBox4.Visible = True
  59.         GroupBox4.Location = New Point(0, 358)
  60.         GroupBox5.Visible = False
  61.     End Sub
  62.  
  63.     Private Sub GradeLevelsBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GradeLevelsBox.SelectedIndexChanged
  64.         GroupBox3.Text = GradeLevelsBox.SelectedItem
  65.         If GradeLevelsBox.SelectedItem = Nothing Then
  66.             MandatoryClassBox.Enabled = False
  67.             OptionalClassBox.Enabled = False
  68.             MandatoryTextBox.Enabled = False
  69.             OptionalTextBox.Enabled = False
  70.             MandatoryButton.Enabled = False
  71.             OptionalButton.Enabled = False
  72.             Button3.Enabled = False
  73.             GradeLevelEdit_Load()
  74.         Else
  75.             MandatoryClassBox.Enabled = True
  76.             OptionalClassBox.Enabled = True
  77.             MandatoryTextBox.Enabled = True
  78.             OptionalTextBox.Enabled = True
  79.             MandatoryButton.Enabled = True
  80.             OptionalButton.Enabled = True
  81.             Button3.Enabled = True
  82.             MandatoryClassBox.Items.Clear()
  83.             OptionalClassBox.Items.Clear()
  84.             GradeLevelEdit_Load()
  85.         End If
  86.     End Sub
  87.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  88.    'Section 2: Editing Schedule Properties
  89.  
  90.     Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
  91.         If CheckBox1.Checked Then
  92.             LunchPeriodLength.Enabled = False
  93.         Else
  94.             LunchPeriodLength.Enabled = True
  95.         End If
  96.     End Sub
  97.  
  98.     Private Sub PeriodNumber_ValueChanged(sender As Object, e As EventArgs) Handles PeriodNumber.ValueChanged
  99.         If PeriodNumber.Value < MandatoryClassBox.Items.Count Then
  100.             PeriodNumberError.Visible = True
  101.         Else
  102.             PeriodNumberError.Visible = False
  103.         End If
  104.     End Sub
  105.  
  106.     Private Sub DeveloperLogsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DeveloperLogsToolStripMenuItem.Click
  107.         DevLogs.Show()
  108.     End Sub
  109.     '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  110.    'Section 3: Editing Grade Levels
  111.  
  112.     Private Sub MandatoryButton_Click(sender As Object, e As EventArgs) Handles MandatoryButton.Click
  113.         Dim mc As XElement = New XElement("mandatory-class")
  114.         Dim pass As Boolean = True
  115.         For Each x As String In MandatoryClassBox.Items
  116.             If x.Equals(MandatoryTextBox.Text) Or x.Equals(OptionalTextBox.Text) Then
  117.                 MsgBox("There is already a class with the same name.", MsgBoxStyle.Critical)
  118.                 pass = False
  119.             End If
  120.         Next
  121.         For Each x As String In OptionalClassBox.Items
  122.             If x.Equals(MandatoryTextBox.Text) Or x.Equals(OptionalTextBox.Text) Then
  123.                 MsgBox("There is already a class with the same name.", MsgBoxStyle.Critical)
  124.                 pass = False
  125.             End If
  126.         Next
  127.         If pass = True Then
  128.             mc.SetAttributeValue("name", MandatoryTextBox.Text)
  129.             MandatoryClassBox.Items.Add(MandatoryTextBox.Text)
  130.             MandatoryTextBox.Text = ""
  131.             OptionalTextBox.Text = ""
  132.             For Each a As XElement In doc.Root.Descendants()
  133.                 If a.Name = "gradelevel" Then
  134.                     If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  135.                         a.Add(mc)
  136.                     End If
  137.                 End If
  138.             Next
  139.             If PeriodNumber.Value < MandatoryClassBox.Items.Count Then
  140.                 PeriodNumberError.Visible = True
  141.             Else
  142.                 PeriodNumberError.Visible = False
  143.             End If
  144.         End If
  145.     End Sub
  146.  
  147.     Private Sub OptionalButton_Click(sender As Object, e As EventArgs) Handles OptionalButton.Click
  148.         Dim oc As XElement = New XElement("optional-class")
  149.         Dim pass As Boolean = True
  150.         For Each x As String In OptionalClassBox.Items
  151.             If x.Equals(OptionalTextBox.Text) Or x.Equals(MandatoryTextBox.Text) Then
  152.                 MsgBox("There is already a class with the same name.", MsgBoxStyle.Critical)
  153.                 pass = False
  154.             ElseIf x.Equals("") Then
  155.                 MsgBox("The class name is empty.", MsgBoxStyle.Critical)
  156.             End If
  157.         Next
  158.         For Each x As String In MandatoryClassBox.Items
  159.             If x.Equals(OptionalTextBox.Text) Or x.Equals(MandatoryTextBox.Text) Then
  160.                 MsgBox("There is already a class with the same name.", MsgBoxStyle.Critical)
  161.                 pass = False
  162.             ElseIf x.Equals("") Then
  163.                 MsgBox("The class name is empty.", MsgBoxStyle.Critical)
  164.             End If
  165.         Next
  166.         If pass = True Then
  167.             oc.SetAttributeValue("name", OptionalTextBox.Text)
  168.             OptionalClassBox.Items.Add(OptionalTextBox.Text)
  169.             MandatoryTextBox.Text = ""
  170.             OptionalTextBox.Text = ""
  171.             For Each a As XElement In doc.Root.Descendants()
  172.                 If a.Name = "gradelevel" Then
  173.                     If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  174.                         a.Add(oc)
  175.                     End If
  176.                 End If
  177.             Next
  178.         End If
  179.     End Sub
  180.  
  181.     Private Sub MandatoryClassBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles MandatoryClassBox.SelectedIndexChanged
  182.         Try
  183.             GroupBox4.Enabled = True
  184.             GroupBox4.Text = MandatoryClassBox.SelectedItem.ToString()
  185.             ClassLevels.Items.Clear()
  186.             For Each a As XElement In doc.Root.Elements()
  187.                 'MsgBox("I can hear the drummers drumming: " + a.ToString)
  188.                If a.Name = "gradelevel" Then
  189.                     If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  190.                         'MsgBox("And the trumpets someone's trying to summon someone.")
  191.                        For Each b As XElement In a.Elements()
  192.                             'MsgBox("I know something's coming: " + b.ToString)
  193.                            If b.FirstAttribute.Value = GroupBox4.Text Then
  194.                                 'MsgBox("But I'm running from it to be standing at the summit: " + b.ToString)
  195.                                For Each c As XElement In b.Elements()
  196.                                     'MsgBox("Here to stay! " + c.ToString)
  197.                                    If c.Name = "level" Then
  198.                                         'MsgBox("Oh, give me the beat, boys, and free my soul: " + c.ToString)
  199.                                        If c.FirstAttribute.Name = "name" Then
  200.                                             'MsgBox("I wanna get lost in your rock and roll: " + c.ToString)
  201.                                            If c.FirstAttribute.Value = "singlelevel" Then
  202.                                                 SingleLevel.Checked = True
  203.                                             Else
  204.                                                 'MsgBox("and drift away: " + c.ToString)
  205.                                                SingleLevel.Checked = False
  206.                                                 ClassLevels.Items.Add(c.FirstAttribute.Value)
  207.                                             End If
  208.                                         End If
  209.                                     End If
  210.                                 Next
  211.                             End If
  212.                         Next
  213.                     End If
  214.                 End If
  215.             Next
  216.         Catch ex As NullReferenceException
  217.  
  218.         End Try
  219.     End Sub
  220.  
  221.     Private Sub OptionalClassBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles OptionalClassBox.SelectedIndexChanged
  222.         Try
  223.             GroupBox4.Enabled = True
  224.             GroupBox4.Text = OptionalClassBox.SelectedItem.ToString()
  225.         Catch ex As NullReferenceException
  226.  
  227.         End Try
  228.     End Sub
  229.  
  230.     Private Sub GradeLevelEdit_Closing(sender As Object, e As EventArgs) Handles MyBase.Closing
  231.         doc.Root.Save("ScheduleConfig.xml")
  232.         firstschedule.Root.Save("FirstSchedule.xml")
  233.     End Sub
  234.  
  235.  
  236.     Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
  237.         For Each a As XElement In doc.Root.Elements()
  238.             If a.HasAttributes Then
  239.                 If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  240.                     GradeLevelsBox.Items.Remove(GradeLevelsBox.SelectedItem)
  241.                     a.Remove()
  242.                 End If
  243.             End If
  244.         Next
  245.     End Sub
  246.  
  247.     Private Sub GradeLevelEdit_Load()
  248.         For Each a As XElement In doc.Root.Elements()
  249.             If a.HasAttributes = True Then
  250.                 If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  251.                     For Each b As XElement In a.Elements()
  252.                         If b.Name = "mandatory-class" Then
  253.                             MandatoryClassBox.Items.Add(b.FirstAttribute.Value)
  254.                         End If
  255.                         If b.Name = "optional-class" Then
  256.                             OptionalClassBox.Items.Add(b.FirstAttribute.Value)
  257.                         End If
  258.                     Next
  259.                 End If
  260.             End If
  261.         Next
  262.     End Sub
  263.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  264.    'Section 4: Editing Class Properties
  265.  
  266.     Private Sub ClassLevels_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ClassLevels.SelectedIndexChanged
  267.         If SingleLevel.Checked Then
  268.             ClassLevels.Enabled = False
  269.             ClassLevelsTextBox.Enabled = False
  270.             ClassLevelsButton.Enabled = False
  271.         Else
  272.             ClassLevels.Enabled = True
  273.             ClassLevelsTextBox.Enabled = True
  274.             ClassLevelsButton.Enabled = True
  275.         End If
  276.     End Sub
  277.  
  278.     Private Sub ClassLevelsButton_Click(sender As Object, e As EventArgs) Handles ClassLevelsButton.Click
  279.         Dim pass As Boolean = True
  280.         For Each x As String In ClassLevels.Items
  281.             If x.Equals(ClassLevelsTextBox.Text) Then
  282.                 MsgBox("There is already a class level with the same name.", MsgBoxStyle.Critical)
  283.                 pass = False
  284.             ElseIf x.Equals("") Then
  285.                 MsgBox("The class level is empty.", MsgBoxStyle.Critical)
  286.                 pass = False
  287.             ElseIf x.Equals("singlelevel") Then
  288.                 MsgBox("singlelevel is a reserved name that cannot be used.", MsgBoxStyle.Critical)
  289.                 pass = False
  290.             End If
  291.         Next
  292.         If pass = True Then
  293.             ClassLevels.Items.Add(ClassLevelsTextBox.Text)
  294.             Dim lv As XElement = New XElement("level")
  295.             lv.SetAttributeValue("name", ClassLevelsTextBox.Text)
  296.             For Each a As XElement In doc.Root.Elements()
  297.                 If a.HasAttributes Then
  298.                     If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  299.                         For Each b As XElement In a.Elements()
  300.                             If b.FirstAttribute.Value = GroupBox4.Text Then
  301.                                 b.Add(lv)
  302.                             End If
  303.                         Next
  304.                     End If
  305.                 End If
  306.  
  307.             Next
  308.         End If
  309.     End Sub
  310.  
  311.     Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  312.         Try
  313.             MandatoryClassBox.Items.Remove(MandatoryClassBox.SelectedItem)
  314.             OptionalClassBox.Items.Remove(OptionalClassBox.SelectedItem)
  315.             For Each a As XElement In doc.Root.Elements()
  316.                 'MsgBox("And we were trying different things: " + a.ToString)
  317.                If a.Name = "gradelevel" Then
  318.                     'MsgBox("We were smoking funny things: " + a.ToString)
  319.                    If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  320.                         For Each b As XElement In a.Elements()
  321.                             'MsgBox("Making love out by the lake to our favorite song: " + b.ToString())
  322.                            If b.FirstAttribute.Value = GroupBox4.Text Then
  323.                                 'MsgBox("Sipping whiskey out the bottle, not thinking 'bout tomorrow: " + b.ToString())
  324.                                b.Remove()
  325.                             End If
  326.                         Next
  327.                     End If
  328.                 End If
  329.             Next
  330.         Catch ex As NullReferenceException
  331.             MsgBox("There is no course to delete", MsgBoxStyle.Critical)
  332.         End Try
  333.  
  334.     End Sub
  335.  
  336.     Private Sub SingleLevel_CheckedChanged(sender As Object, e As EventArgs) Handles SingleLevel.CheckedChanged
  337.         If SingleLevel.Checked Then
  338.             For Each a As XElement In doc.Root.Elements()
  339.                 Dim sl As XElement = New XElement("level")
  340.                 sl.SetAttributeValue("name", "singlelevel")
  341.                 Try
  342.                     If a.HasAttributes Then
  343.                         If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  344.                             For Each b As XElement In a.Elements()
  345.                                 If b.FirstAttribute.Value = GroupBox4.Text Then
  346.                                     b.Value = ""
  347.                                     b.Add(sl)
  348.                                 End If
  349.                             Next
  350.                         End If
  351.                     End If
  352.                 Catch ex As NullReferenceException
  353.                     MsgBox("NullReferenceException caught! " + a.ToString)
  354.                 End Try
  355.             Next
  356.             ClassLevelsTextBox.Enabled = False
  357.             ClassLevels.Enabled = False
  358.             ClassLevelsButton.Enabled = False
  359.         Else
  360.             ClassLevelsTextBox.Enabled = True
  361.             ClassLevels.Enabled = True
  362.             ClassLevelsButton.Enabled = True
  363.             For Each a As XElement In doc.Root.Elements()
  364.                 Dim sl As XElement = New XElement("level")
  365.                 sl.SetAttributeValue("name", "singlelevel")
  366.                 If a.FirstAttribute.Value = GradeLevelsBox.SelectedItem Then
  367.                     For Each b As XElement In a.Elements()
  368.                         If b.FirstAttribute.Value = GroupBox4.Text Then
  369.                             b.Value = ""
  370.                         End If
  371.                     Next
  372.                 End If
  373.             Next
  374.         End If
  375.     End Sub
  376.     Dim check As Boolean = True
  377.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  378.         If check = True Then
  379.             GroupBox3.Visible = False
  380.             GroupBox2.Visible = False
  381.             GroupBox4.Visible = False
  382.             GroupBox5.Visible = True
  383.             Button1.Text = "Click for Courses"
  384.             Dim fn As String = ""
  385.             Dim ln As String = ""
  386.             For Each a As XElement In doc.Root.Elements()
  387.                 If a.Name = "gradelevel" Then
  388.                     For Each b As XElement In a.Elements()
  389.                         If b.Name.ToString().Contains("-class") Then
  390.                             If b.FirstAttribute.Name = "name" Then
  391.                                 CertifiedClasses.Items.Remove(b.FirstAttribute.Value)
  392.                             End If
  393.                         End If
  394.                     Next
  395.                 End If
  396.                 If a.Name.ToString().Contains("teachers") Then
  397.                     For Each b As XElement In a.Elements()
  398.                         If b.Name.ToString.Contains("teacher") Then
  399.                             For Each c As XElement In b.Elements()
  400.                                 If c.Name.ToString.Contains("firstname") Then
  401.                                     fn = c.Value.ToString
  402.                                 End If
  403.                                 If c.Name.ToString.Contains("lastname") Then
  404.                                     ln = c.Value.ToString
  405.                                 End If
  406.                                 If c.Name.ToString.Contains("certification") = False Then
  407.                                     If ln.Equals("") = False Then
  408.                                         TeacherDirectory.Items.Remove(fn + " " + ln)
  409.                                         fn = ""
  410.                                         ln = ""
  411.                                     End If
  412.                                 End If
  413.                             Next
  414.                         End If
  415.                     Next
  416.  
  417.                 End If
  418.             Next
  419.             For Each a As XElement In doc.Root.Elements()
  420.                 If a.Name = "gradelevel" Then
  421.                     For Each b As XElement In a.Elements()
  422.                         If b.Name.ToString().Contains("-class") Then
  423.                             If b.FirstAttribute.Name = "name" Then
  424.                                 CertifiedClasses.Items.Add(b.FirstAttribute.Value, False)
  425.                             End If
  426.                         End If
  427.                     Next
  428.                 End If
  429.                 If a.Name.ToString().Contains("teachers") Then
  430.                     For Each b As XElement In a.Elements()
  431.                         If b.Name.ToString.Contains("teacher") Then
  432.                             For Each c As XElement In b.Elements()
  433.                                 If c.Name.ToString.Contains("firstname") Then
  434.                                     fn = c.Value.ToString
  435.                                 End If
  436.                                 If c.Name.ToString.Contains("lastname") Then
  437.                                     ln = c.Value.ToString
  438.                                 End If
  439.                                 If c.Name.ToString.Contains("certification") = False Then
  440.                                     If ln.Equals("") = False Then
  441.                                         TeacherDirectory.Items.Add(fn + " " + ln)
  442.                                         fn = ""
  443.                                         ln = ""
  444.                                     End If
  445.                                 End If
  446.                             Next
  447.                         End If
  448.                     Next
  449.  
  450.                 End If
  451.             Next
  452.             check = False
  453.         Else
  454.             GroupBox3.Visible = True
  455.             GroupBox2.Visible = True
  456.             GroupBox4.Visible = True
  457.             GroupBox5.Visible = False
  458.             Button1.Text = "Click for Teachers"
  459.             check = True
  460.         End If
  461.  
  462.     End Sub
  463.  
  464.     Private Sub CheckBox2_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox2.CheckedChanged
  465.         If CheckBox2.CheckState = CheckState.Checked Then
  466.             MinParccScore.Enabled = True
  467.         Else
  468.             MinParccScore.Enabled = False
  469.         End If
  470.     End Sub
  471.  
  472.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  473.    'Section 5: Adding and Editing Teachers
  474.  
  475.     Private Sub AddTeacher_Click(sender As Object, e As EventArgs) Handles AddTeacher.Click
  476.         Dim te As XElement = New XElement("teacher") '<teacher>
  477.        Dim fn As XElement = New XElement("firstname") '<firstname>
  478.        Dim ln As XElement = New XElement("lastname") '<lastname>
  479.  
  480.         Dim pass As Boolean = True 'Valid name test
  481.        For Each x As String In TeacherDirectory.Items 'Iterator loop for the TeacherDirectory ListBox
  482.            If x.ToString.Equals(FirstName.Text + " " + LastName.Text) Then 'Is there a teacher with the same name?
  483.                MsgBox("There is already a teacher with the same name.", MsgBoxStyle.Critical) 'Error Message
  484.                pass = False 'Failed
  485.            ElseIf FirstName.Text.Equals("") Then 'Is the first name empty?
  486.                MsgBox("The teacher has an empty first name.", MsgBoxStyle.Critical) 'Error Message
  487.                pass = False 'Failed
  488.            ElseIf LastName.Text.Equals("") Then 'Is the last name empty?
  489.                MsgBox("The teacher has an empty last name.", MsgBoxStyle.Critical) 'Error Message
  490.                pass = False 'Failed
  491.            End If
  492.         Next
  493.         If pass = True Then 'If you passed the test
  494.            TeacherDirectory.Items.Add(FirstName.Text + " " + LastName.Text) 'Add the new teacher's name to the TeacherDirectory
  495.            For Each a As XElement In doc.Root.Elements() 'Iterate through the document
  496.                If a.Name.ToString().Contains("teachers") Then 'Find <teachers>
  497.                    a.Add(te) 'Add the <teacher> tag to <teachers>
  498.                    te.Add(fn) 'Add the <firstname> tag inside of <teacher>
  499.                    te.Add(ln) 'Add the <lastname> tag inside of <teacher>
  500.                    fn.Add(FirstName.Text) 'Add the new teacher's first name inside of <firstname>
  501.                    ln.Add(LastName.Text) 'Add the new teacher's last name inside of <lastname>
  502.                End If
  503.             Next
  504.             Dim b As Object() = CertifiedClasses.CheckedItems.OfType(Of Object).ToArray() 'Make a new array with all of the items in the CertifiedClasses CheckedListBox
  505.            For c As Integer = 0 To b.Count() - 1 'Start a for loop that iterates through the elements of b()
  506.                Try 'There is a possibility of an IndexOutOfRangeException
  507.                    Dim cr As XElement = New XElement("certification") '<certification>
  508.                    te.Add(cr) 'Add <certification> inside of <teacher>
  509.                    cr.Add(CertifiedClasses.CheckedItems.Item(c).ToString) 'Add the checked item with the index of c inside of <certification>
  510.                Catch ex As IndexOutOfRangeException
  511.                     MsgBox("Index out of range", MsgBoxStyle.Exclamation) 'IndexOutOfRangeException
  512.                End Try
  513.             Next
  514.         End If
  515.         doc.Root.Save("ScheduleConfig.xml")
  516.     End Sub
  517.  
  518.     Private Sub TeacherDirectory_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TeacherDirectory.SelectedIndexChanged
  519.         Dim fn As String = "" 'First Name
  520.        Dim ln As String = "" 'Last Name
  521.        Dim ct As Integer = 0 'Counter
  522.        Dim fnright As Boolean = False 'First Name Selected
  523.        Dim lnright As Boolean = False 'Last Name Selected
  524.        Try
  525.             For Each a As String In TeacherDirectory.SelectedItem.ToString.Split(" ") 'Split up the first name and last name
  526.                If ct = 0 Then 'If a is the first name
  527.                    fn = a 'Set fn to a
  528.                ElseIf ct = 1 Then 'If a is the last name
  529.                    ln = a 'Set ln to a
  530.                End If
  531.                 ct = ct + 1 'Change a from first name to last name
  532.            Next
  533.         Catch ex As NullReferenceException 'When a NullReferenceException occurs, ignore it
  534.  
  535.         End Try
  536.         For Each a As XElement In doc.Root.Elements() 'Iterate through the document
  537.            If a.Name.ToString.Contains("teachers") Then 'If <teachers> is found
  538.                For Each b As XElement In a.Elements() 'Iterate through <teachers>
  539.                    If b.Name.ToString.Contains("teacher") Then 'If <teacher> is found
  540.                        For Each c As XElement In b.Elements() 'Iterate through <teacher>
  541.                            If c.Name.ToString.Contains("certification") Then 'If <certification> is found
  542.                                'MsgBox("Don't know what you got till it's gone: " + c.Name.ToString)
  543.                                Dim d As Object() = CertifiedClasses.Items.OfType(Of Object).ToArray() 'Make a new array with all of the items in the CertifiedClasses CheckedListBox
  544.                                For Each od In d 'Iterate through d()
  545.                                    CertifiedClasses.SetItemChecked(CertifiedClasses.Items.IndexOf(od), False) 'Uncheck the item
  546.                                Next
  547.                             End If
  548.                         Next
  549.                     End If
  550.                 Next
  551.             End If
  552.         Next
  553.         For Each a As XElement In doc.Root.Elements() 'Iterate through the document
  554.            If a.Name.ToString().Contains("teachers") Then 'If <teachers> is found
  555.                For Each b As XElement In a.Elements() 'Iterate through <teachers>
  556.                    If b.Name.ToString.Contains("teacher") Then 'If <teacher> is found
  557.                        For Each c As XElement In b.Elements() 'Iterate through <teacher>
  558.                            If c.Name.ToString.Contains("firstname") Then 'If <firstname> is found
  559.                                If c.Value.ToString.Equals(fn) Then 'If <firstname> is equal to fn
  560.                                    fnright = True 'fnright is true
  561.                                Else 'Or else
  562.                                    fnright = False 'fnright is false
  563.                                End If
  564.                             End If
  565.                             If c.Name.ToString.Contains("lastname") Then 'If <lastname> is found
  566.                                If c.Value.ToString.Equals(ln) Then 'If <lastname> is equal to ln
  567.                                    lnright = True 'lnright is true
  568.                                Else 'Or else
  569.                                    fnright = False 'lnright is false
  570.                                End If
  571.                             End If
  572.                             If c.Name.ToString.Contains("certification") = True Then 'If <certification> is found
  573.                                'If a course has been deleted, scan through this checklist to remove it from the checklist and any of the teacher's XML elements.
  574.                                Dim d As Object() = CertifiedClasses.Items.OfType(Of Object).ToArray() 'Make a new array with all of the items in the CertifiedClasses CheckedListBox
  575.                                For Each od In d 'Iterate through d()
  576.                                    If od.Contains(c.Value.ToString) Then 'If an item in CertifiedClasses is equal to the value of <certification>
  577.                                        If VerifyCourseExistence(od) = True Then 'Verify the course existence
  578.                                            For Each f As XElement In c.Ancestors() 'Iterate through the ancestors of <certification>
  579.                                                If f.Name.ToString.Equals("teacher") Then 'If <teacher> is found
  580.                                                    FirstName.Text = fn 'Set the FirstName textbox to fn
  581.                                                    LastName.Text = ln 'Set the LastName textbox to ln
  582.                                                    If f.HasElements = True Then 'If there are elements in f
  583.                                                        For Each g As XElement In f.Elements() 'Iterate through <teacher>
  584.                                                            If f.Element("firstname").Value.ToString.Contains(FirstName.Text) Then 'If the value of <firstname> is equal to the textbox
  585.                                                                If f.Element("lastname").Value.ToString.Contains(LastName.Text) Then 'If the value of <lastname> is equal to the textbox
  586.                                                                    CertifiedClasses.SetItemChecked(CertifiedClasses.Items.IndexOf(od), True) 'Set the CertifiedClasses item with the index of od to a checked state
  587.                                                                End If
  588.                                                             End If
  589.                                                         Next
  590.                                                     End If
  591.                                                 End If
  592.                                             Next
  593.                                         Else
  594.                                             c.Remove() 'If the course does not exist, then remove the <certification> node with the inexistent course
  595.                                        End If
  596.                                     End If
  597.                                 Next
  598.                             End If
  599.                         Next
  600.                     End If
  601.                 Next
  602.             End If
  603.         Next
  604.  
  605.     End Sub
  606.     Function VerifyCourseExistence(ByVal course As Object)
  607.         For Each a As XElement In doc.Root.Elements() 'Iterate through <teachers>
  608.            If a.Name.ToString().Contains("gradelevel") Then 'If <gradelevel> is found
  609.                For Each b As XElement In a.Elements() 'Iterate through <gradelevel>
  610.                    If b.FirstAttribute.Value.ToString.Contains(course.ToString) Then 'If an element's first attribute (name) is equal to the variable course in the parameters
  611.                        Return True 'Return True
  612.                    End If
  613.                 Next
  614.             End If
  615.         Next
  616.         Return False 'When the course is not found, it will end here and Return False
  617.    End Function
  618.  
  619.     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
  620.         Dim fnright As Boolean = False
  621.         Dim lnright As Boolean = False
  622.         'Check to make sure there is a teacher to write to
  623.        Dim pass As Boolean = False
  624.         For Each x As String In TeacherDirectory.Items 'Iterator loop for the TeacherDirectory ListBox
  625.            If x.ToString.Equals(FirstName.Text + " " + LastName.Text) Then 'Is there a teacher with the same name?
  626.                pass = True
  627.             End If
  628.         Next
  629.         If pass = False Then
  630.             MsgBox("There is no teacher to write to.", MsgBoxStyle.Critical) 'Error Message
  631.        Else
  632.             For Each a As XElement In doc.Root.Elements()
  633.                 If a.Name.ToString.Contains("teachers") Then
  634.                     For Each b As XElement In a.Elements()
  635.                         If b.Name.ToString.Contains("teacher") Then
  636.                             Dim count As Integer = 1
  637.                             For Each c As String In TeacherDirectory.SelectedItem.ToString.Split(" ")
  638.                                 If count = 1 Then
  639.                                     For Each d As XElement In b.Elements()
  640.                                         If d.Name = "firstname" Then
  641.                                             If d.Value.ToString.Contains(c) Then
  642.                                                 fnright = True
  643.                                             End If
  644.                                         End If
  645.                                     Next
  646.                                 ElseIf count = 2 Then
  647.                                     For Each d As XElement In b.Elements()
  648.                                         If d.Name = "lastname" Then
  649.                                             If d.Value.ToString.Contains(c) Then
  650.                                                 lnright = True
  651.                                             End If
  652.                                         End If
  653.                                     Next
  654.                                 End If
  655.                                 count = count + 1
  656.                             Next
  657.                             If fnright = True And lnright = True Then
  658.                                 b.Remove()
  659.                             End If
  660.                         End If
  661.                     Next
  662.                 End If
  663.             Next
  664.             Dim te As XElement = New XElement("teacher") '<teacher>
  665.            Dim fn As XElement = New XElement("firstname") '<firstname>
  666.            Dim ln As XElement = New XElement("lastname") '<lastname>
  667.            Dim pass2 As Boolean = True
  668.  
  669.             If pass2 = True Then 'If you passed the test
  670.                For Each a As XElement In doc.Root.Elements() 'Iterate through the document
  671.                    If a.Name.ToString().Contains("teachers") Then 'Find <teachers>
  672.                        a.Add(te) 'Add the <teacher> tag to <teachers>
  673.                        te.Add(fn) 'Add the <firstname> tag inside of <teacher>
  674.                        te.Add(ln) 'Add the <lastname> tag inside of <teacher>
  675.                        fn.Add(FirstName.Text) 'Add the new teacher's first name inside of <firstname>
  676.                        ln.Add(LastName.Text) 'Add the new teacher's last name inside of <lastname>
  677.                    End If
  678.                 Next
  679.                 Dim b As Object() = CertifiedClasses.CheckedItems.OfType(Of Object).ToArray() 'Make a new array with all of the items in the CertifiedClasses CheckedListBox
  680.                For c As Integer = 0 To b.Count() - 1 'Start a for loop that iterates through the elements of b()
  681.                    Try 'There is a possibility of an IndexOutOfRangeException
  682.                        Dim cr As XElement = New XElement("certification") '<certification>
  683.                        te.Add(cr) 'Add <certification> inside of <teacher>
  684.                        cr.Add(CertifiedClasses.CheckedItems.Item(c).ToString) 'Add the checked item with the index of c inside of <certification>
  685.                    Catch ex As IndexOutOfRangeException
  686.                         MsgBox("Index out of range", MsgBoxStyle.Exclamation) 'IndexOutOfRangeException
  687.                    End Try
  688.                 Next
  689.             End If
  690.             doc.Root.Save("ScheduleConfig.xml")
  691.         End If
  692.     End Sub
  693.  
  694.     Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
  695.         Dim fnright As Boolean = False
  696.         Dim lnright As Boolean = False
  697.         For Each a As XElement In doc.Root.Elements()
  698.             If a.Name.ToString.Contains("teachers") Then
  699.                 For Each b As XElement In a.Elements()
  700.                     If b.Name.ToString.Contains("teacher") Then
  701.                         Dim count As Integer = 1
  702.                         For Each c As String In TeacherDirectory.SelectedItem.ToString.Split(" ")
  703.                             If count = 1 Then
  704.                                 For Each d As XElement In b.Elements()
  705.                                     If d.Name = "firstname" Then
  706.                                         If d.Value.ToString.Contains(c) Then
  707.                                             fnright = True
  708.                                         End If
  709.                                     End If
  710.                                 Next
  711.                             ElseIf count = 2 Then
  712.                                 For Each d As XElement In b.Elements()
  713.                                     If d.Name = "lastname" Then
  714.                                         If d.Value.ToString.Contains(c) Then
  715.                                             lnright = True
  716.                                         End If
  717.                                     End If
  718.                                 Next
  719.                             End If
  720.                             count = count + 1
  721.                         Next
  722.                         If fnright = True And lnright = True Then
  723.                             TeacherDirectory.Items.Remove(TeacherDirectory.SelectedItem)
  724.                             b.Remove()
  725.                         End If
  726.                     End If
  727.                 Next
  728.             End If
  729.         Next
  730.     End Sub
  731.     ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  732.    'Section 6: Generating The Schedule
  733.    Private Sub Button6_Click() Handles Button6.Click
  734.         'After each generated class, remove it from the list
  735.  
  736.         'Dim ync As MsgBoxResult = MsgBox("Would you like your schedule to be generated through a simulation?", MsgBoxStyle.YesNoCancel)
  737.        'If ync = MsgBoxResult.Yes Then
  738.        Dim pn As Integer = PeriodNumber.Value 'pn is a shortcut to PeriodNumber.Value (how many periods are in a day)
  739.        If NumberOfStudents.Value < 1 Then 'If you have 0 or a negative number of students
  740.            MsgBox("You have less than 1 student. That's a lotta damage.", MsgBoxStyle.Critical)
  741.         Else 'If you have at least 1 student
  742.            Dim studentid As Integer = 1 'Student ID
  743.            For Each a As XElement In doc.Root.Elements 'Iterate through the document
  744.                For std As Integer = 1 To NumberOfStudents.Value 'Use std as a variable for the nth student in the grade level
  745.                    If a.Name = "gradelevel" Then 'If you found the <gradelevel> element
  746.                        Dim gradename As String = "" 'Make gradename empty because the name of the grade has not been determined yet
  747.  
  748.                         Randomize() 'Call the random number generator (I don't even think it's required)
  749.                        Dim genmath As Integer = CInt((Int(10 * Rnd()) + 1) + 10) 'Not used at the moment
  750.                        Dim enrmath As Integer = CInt((Int((28 - genmath) * Rnd()) + 1)) 'Not used at the moment
  751.                        Dim aermath As Integer = CInt(Int(30 - (genmath + enrmath))) 'Not used at the moment
  752.  
  753.                         Dim genspan As Integer = CInt((Int(10 * Rnd()) + 1) + 15) 'Not used at the moment
  754.                        Dim enrspan As Integer = CInt(Int(30 - genspan)) 'Not used at the moment
  755.  
  756.                         If a.HasAttributes Then 'If <gradelevel> has an attribute
  757.                            If a.FirstAttribute.Name = "name" Then 'If the attribute is name=""
  758.                                gradename = a.FirstAttribute.Value 'Set gradename to the value of the name="" attribute
  759.                            End If
  760.                         End If
  761.                         Dim stu As XElement = New XElement("student") '<student> element
  762.                        stu.Add(New XAttribute("grade", gradename.ToString)) 'Add grade="" attribute to <student> which has the value of the grade level of the student
  763.                        stu.Add(New XAttribute("id", studentid.ToString)) 'Add id="" attribute to <student> which has the value of the Student ID
  764.                        'MsgBox(stu.ToString)
  765.                        firstschedule.Root.Add(stu) 'Add the <student> element to FirstSchedule.xml
  766.                        Dim clsofgrd As Object() = a.Elements().OfType(Of Object).ToArray 'Put the elements inside of <gradelevel> into an array
  767.                        For c As Integer = 1 To pn 'Make a for loop that goes from 1 to the period number
  768.                            Dim clscnt As Integer = 0 'Detects how many mandatory classes are remaining to be scheduled (0 by default)
  769.                            For Each b As XElement In a.Elements() 'Iterate through the elements of <gradelevel>
  770.                                If b.Name = "mandatory-class" Then 'If <mandatory-class> is found
  771.                                    clscnt = clscnt + 1 'Increase clscnt by 1
  772.                                End If
  773.                             Next
  774.                             Dim cls As Integer = CInt(Int(clscnt * Rnd()) + 0) 'Set a random number from 0 to the number of classes there are
  775.                            Try
  776.                                 If clsofgrd(cls).Equals(Nothing) Then
  777.                                     MsgBox("Double whammy")
  778.                                 End If
  779.                             Catch ex As IndexOutOfRangeException
  780.  
  781.                                 Continue For
  782.                             End Try
  783.  
  784.                             Try 'There will be an IndexOutOfRangeException
  785.                                'MsgBox(clsofgrd.GetValue(cls).ToString)
  786.                                stu.Add(New XElement("p" + c.ToString, clsofgrd.GetValue(cls))) 'Add the <p*> element to the student which contains the class picked in the random number generator on line 774
  787.                                'DELETE THE SELECTED INDEX FROM THE ARRAY clsofgrd
  788.                                clsofgrd = clsofgrd.Where(Function(w) w IsNot clsofgrd(cls)).ToArray
  789.                                 'MsgBox(clsofgrd(cls))
  790.                                clscnt = clscnt - 1 'You added a mandatory class, so reduce this by 1
  791.                            Catch ex As IndexOutOfRangeException
  792.                                 stu.Add(New XElement("MoreProblems", stu.ToString))
  793.                                 'MsgBox("Index outta range: " + cls.ToString + " : " + clscnt.ToString)
  794.                            End Try
  795.  
  796.                         Next
  797.                         'MsgBox(genmath.ToString + " " + enrmath.ToString + " " + aermath.ToString)
  798.                    End If
  799.                     studentid = studentid + 1
  800.                 Next
  801.  
  802.             Next
  803.         End If
  804.  
  805.  
  806.         'End If
  807.    End Sub
  808. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement