public NewIdentificationUnitDialog(CollectionSpecimen spec, IdentificationUnits ius)
            : this(true)
        {
            if (ius != null && spec != null)
            {
                this.cs = spec;
                this._ius = ius;

                this.comboBoxTaxonomicGroup_TextChanged(null, null);

                this.parent = null;
                try
                {
                    tax = DataFunctions.Instance.CreateTaxonNames();
                    this.buttonOk.Enabled = false;
                    this.buttonNewAnalysis.Enabled = false;
                    this.comboBoxRelation.Enabled = false;
                    this.fillTaxonomicGroupList();
                    if (this.comboBoxTaxonomicGroup.Items.Count > 0)
                    {
                        this.comboBoxTaxonomicGroup.SelectedItem = this.comboBoxTaxonomicGroup.Items[0];
                    }

                    // Liste aller IdentificationUnits des aktuellen Specimen, um DiplayOrder zu finden
                    short order = (short)DataFunctions.Instance.RetrieveMaxDisplayOrderCollectionSpecimenIdentificationUnit(spec);
                    if (order > 0)
                        this.displayOrder = (short)(order + 1);
                    else
                        this.displayOrder = 1;
                }
                catch (ConnectionCorruptedException ex)
                {
                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    this.Close();
                }
            }
            else
                this.Close();
        }
        public NewIdentificationUnitDialog(CollectionSpecimen spec, IdentificationUnit parentUnit, IdentificationUnits ius)
            : this(spec, ius)
        {
            try
            {
                this.parent = parentUnit;
                if (this.parent != null)
                {
                    this.comboBoxTaxonomicGroup.SelectedItem = this._taxGroup = parent.TaxonomicGroup;

                    if (parent.LastIdentificationCache != null)
                    {
                        this.comboBoxIdentification.Items.Add(parent.LastIdentificationCache);
                        this.comboBoxIdentification.SelectedItem = this._lastIdentification = parent.LastIdentificationCache;
                        TaxonNames taxparent = DataFunctions.Instance.RetrieveTaxon(parent.LastIdentificationCache, parent.TaxonomicGroup);
                        if (taxparent != null)
                            tax = taxparent;
                        else
                            tax.TaxonNameCache = parent.LastIdentificationCache;
                        setSynonym(tax);
                    }
                }

                fillRelationList();

                this.comboBoxRelation.Enabled = true;

                short order = (short)DataFunctions.Instance.RetrieveMaxDisplayOrderCollectionSpecimenIdentificationUnit(spec);
                if (order > 0)
                    this.displayOrder = (short)(order + 1);
                else
                    this.displayOrder = 1;

                this.buttonOk.Enabled = this.setOkButton();
                this.buttonNewAnalysis.Enabled = this.setOkButton();
            }
            catch (ConnectionCorruptedException ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                this.Close();
            }
        }
        /// <summary>
        /// Handles the Click event of the pictureBoxNewSpecimen control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void pictureBoxNewSpecimen_Click(object sender, EventArgs e)
        {
            if (this.treeViewFieldData.Nodes.Count == 0 || this.treeViewFieldData.SelectedNode == null)
            {
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
            TreeNode eventNode = this.treeViewFieldData.SelectedNode;
            TreeViewNodeData eventData = eventNode.Tag as TreeViewNodeData;
            if (eventData == null)
                return;
            if (eventData.NodeType != TreeViewNodeTypes.EventNode)
                return;

            TreeNode seriesNode = eventNode.Parent;
            TreeViewNodeData seriesData = seriesNode.Tag as TreeViewNodeData;
            int? seriesID = seriesData.ID;
            try
            {
                if (seriesData.ID != null)
                {
                    EventSeriess.Instance.Find((int)seriesData.ID);
                    this._events = EventSeriess.Instance.CollectionEvents;

                }
                else
                    this._events = new CollectionEvents(null);
            }
            catch (ConnectionCorruptedException ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return;
            }
            this._events.Find((int)eventData.ID);
            this._specimen = this._events.Specimen;
            if (this._specimen != null)
            {
                CollectionSpecimen cs;
                try
                {
                    cs = this._specimen.CreateSpecimen();
                    this._iu = this._specimen.IdentificationUnits;
                }
                catch (DataFunctionsException ex)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Specimen couldn't be created. (" + ex.Message + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    this.treeViewFieldData.EndUpdate();
                    return;
                }
                this.treeViewFieldData.BeginUpdate();
                TreeNode specNode = new TreeNode();
                this._tvOperations.parametrizeOnlySpecimenNode(cs, specNode);
                this.labelPosition.Text = this._specimen.Position;
                this.treeViewFieldData.SelectedNode.Nodes.Add(specNode);
                this.treeViewFieldData.SelectedNode = specNode;
                if (!this.treeViewFieldData.SelectedNode.IsExpanded)
                {
                    this._expandTrigger = false;
                    this.treeViewFieldData.SelectedNode.ExpandAll();
                    this._expandTrigger = true;
                }
                this.enableDisableButtons(TreeViewNodeTypes.SpecimenNode);
                this._actualLvl = TreeViewNodeTypes.SpecimenNode;
                this.enableDisableToolbarButtons(_actualLvl);
                this.treeViewFieldData.EndUpdate();
            }
            Cursor.Current = Cursors.Default;
        }
        // Auslagern der After Select Funktionalität, um direkten Zugriff zu gewährleisten
        private void afterSelect(TreeNode node)
        {
            Cursor.Current = Cursors.WaitCursor;
            if (node != null)
            {
                TreeViewNodeData data = node.Tag as TreeViewNodeData;
                if (data == null)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("While selecting a node a Display Error occured!", "Display Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    pictureBoxHome_Click(null, null);
                    return;
                }
                TreeNode represent;

                try
                {
                    switch (data.NodeType)
                    {
                        case TreeViewNodeTypes.EventSeriesNode:
                            CollectionEventSeries es = null;
                            if (data.ID != null)
                            {
                                if (EventSeriess.Instance.Find((int)data.ID))
                                {
                                    es = EventSeriess.Instance.Current;
                                    this._events = EventSeriess.Instance.CollectionEvents;
                                }
                                else
                                    throw new ArgumentOutOfRangeException("EventSeries not found");
                            }
                            else
                            {
                                EventSeriess.Instance.Current = null;
                                this._events = new CollectionEvents(null);
                            }
                            this.labelPosition.Text = EventSeriess.Instance.Position;
                            this._actualLvl = TreeViewNodeTypes.EventSeriesNode;
                            represent = this._tvOperations.findRepresentantOfType(node);
                            if (represent.Text.Equals("-1"))
                            {
                                displayEventSeries(es);
                            }
                            else if (represent != this._tvOperations.findRepresentantOfType(this._lastSelection))
                            {
                                try
                                {
                                    displayEventSeries(es);
                                }
                                catch (Exception e)
                                {
                                    Cursor.Current = Cursors.Default;
                                    MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                    pictureBoxHome_Click(null, null);
                                    return;
                                }
                            }
                            this._lastSelection = this.treeViewFieldData.SelectedNode;
                            break;
                        case TreeViewNodeTypes.EventNode:
                            CollectionEvent ce = DataFunctions.Instance.RetrieveCollectionEvent((int)data.ID);
                            if (ce != null)
                            {
                                int? serID = ce.SeriesID;
                                this._events = new CollectionEvents(serID);
                                this._events.Find(ce.CollectionEventID);
                                ce = this._events.Current;
                            }
                            else
                            {
                                Cursor.Current = Cursors.Default;
                                MessageBox.Show("DisplayError", "DisplayError", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                pictureBoxHome_Click(null, null);
                                return;
                            }
                            //Absichern, dass das Event über "+" gefunden wurde.
                            //Absichern, dass das event nicht gefunden wird
                            //Absichern, dass die EventSeries korrekt ist
                            //Absichern, dass specimen belegt ist

                            this._specimen = this._events.Specimen;
                            this.labelPosition.Text = this._events.Position;
                            this._actualLvl = TreeViewNodeTypes.EventNode;
                            represent = this._tvOperations.findRepresentantOfType(node);
                            if (represent.Text.Equals("-1"))
                            {
                                displayEvent(ce);
                            }
                            else if (represent != this._tvOperations.findRepresentantOfType(this._lastSelection))
                            {
                                try
                                {
                                    displayEvent(ce);
                                }
                                catch (Exception e)
                                {
                                    Cursor.Current = Cursors.Default;
                                    MessageBox.Show(e.Message, "DisplayError", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                    pictureBoxHome_Click(null, null);
                                    return;
                                }
                            }
                            this._lastSelection = this.treeViewFieldData.SelectedNode;
                            break;
                        case TreeViewNodeTypes.SpecimenNode:
                            CollectionSpecimen spec = DataFunctions.Instance.RetrieveCollectionSpecimen((int)data.ID);//Wárum DataFunctions?
                            //Absichern, dass das Event über "+" gefunden wurde.
                            //Absichern, dass das event nicht gefunden wird
                            //Absichern, dass die EventSeries korrekt ist
                            //Absichern, dass specimen belegt ist
                            if (spec != null)
                            {
                                int? serID = spec.CollectionEvent.SeriesID;
                                CollectionEvent ev = spec.CollectionEvent;
                                this._events = new CollectionEvents(serID);
                                this._events.Find(ev.CollectionEventID);
                                try
                                {
                                    this._specimen = this._events.Specimen;
                                }
                                catch (DataFunctionsException ex)
                                {
                                    Cursor.Current = Cursors.Default;
                                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                    pictureBoxHome_Click(null, null);
                                    return;
                                }

                                if (!this._specimen.Find(spec.CollectionSpecimenID))
                                {
                                    Cursor.Current = Cursors.Default;
                                    MessageBox.Show("Data of selected Specimen couldn't be retrieved.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                    pictureBoxHome_Click(null, null);
                                    return;
                                }
                                this._iu = this._specimen.IdentificationUnits;
                                spec = this._specimen.Current;//Durch Find erledigt, oder
                                this.labelPosition.Text = this._specimen.Position;
                                this._actualLvl = TreeViewNodeTypes.SpecimenNode;
                                represent = this._tvOperations.findRepresentantOfType(node);
                                if (represent.Text.Equals("-1"))
                                {
                                    displaySpecimen(spec);
                                }
                                else if (represent != this._tvOperations.findRepresentantOfType(this._lastSelection))
                                {
                                    try
                                    {
                                        displaySpecimen(spec);
                                    }
                                    catch (Exception e)
                                    {
                                        Cursor.Current = Cursors.Default;
                                        MessageBox.Show(e.Message, "DisplayError", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                        pictureBoxHome_Click(null, null);
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                Cursor.Current = Cursors.Default;
                                MessageBox.Show("DisplayError", "DisplayError", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                pictureBoxHome_Click(null, null);
                                return;
                            }
                            this._lastSelection = this.treeViewFieldData.SelectedNode;
                            break;
                        case TreeViewNodeTypes.IdentificationUnitNode:
                            IdentificationUnit iu = DataFunctions.Instance.RetrieveIdentificationUnit((int)data.ID);
                            //Absichern, dass das Event über "+" gefunden wurde.
                            //Absichern, dass das event nicht gefunden wird
                            //Absichern, dass die EventSeries korrekt ist
                            //Absichern, dass specimen belegt ist
                            if (iu != null)
                            {
                                int? sID = iu.CollectionSpecimen.CollectionEvent.SeriesID;
                                CollectionEvent evIu = iu.CollectionSpecimen.CollectionEvent;
                                this._events = new CollectionEvents(sID);
                                this._events.Find(evIu.CollectionEventID);
                                try
                                {
                                    this._specimen = this._events.Specimen;
                                }
                                catch (DataFunctionsException ex)
                                {
                                    Cursor.Current = Cursors.Default;
                                    MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                    pictureBoxHome_Click(null, null);
                                    return;
                                }

                                if (!this._specimen.Find(iu.CollectionSpecimenID))
                                {
                                    Cursor.Current = Cursors.Default;
                                    MessageBox.Show("Data of selected Specimen couldn't be retrieved.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                    pictureBoxHome_Click(null, null);
                                    return;
                                }

                                this._iu = this._specimen.IdentificationUnits;

                                if (this._iu == null)
                                {
                                    Cursor.Current = Cursors.Default;
                                    MessageBox.Show("Data of associated Identification Units couldn't be retrieved.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                    pictureBoxHome_Click(null, null);
                                    return;
                                }

                                this._iu.FindTopLevelIU(this.findIUTopLevelID(iu));
                                this._actualLvl = TreeViewNodeTypes.IdentificationUnitNode;
                                this.labelPosition.Text = this._iu.Position;
                                represent = this._tvOperations.findRepresentantOfType(node);
                                if (represent.Text.Equals("-1"))
                                {
                                    displayIdentificationUnit(iu);
                                }
                                else if (represent != this._tvOperations.findRepresentantOfType(this._lastSelection))
                                {
                                    try
                                    {
                                        displayIdentificationUnit(iu);
                                    }
                                    catch (Exception e)
                                    {
                                        Cursor.Current = Cursors.Default;
                                        MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                        pictureBoxHome_Click(null, null);
                                        return;
                                    }
                                }
                            }
                            else
                            {
                                Cursor.Current = Cursors.Default;
                                MessageBox.Show("DisplayError", "DisplayError", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                                pictureBoxHome_Click(null, null);
                                return;
                            }
                            this._lastSelection = this.treeViewFieldData.SelectedNode;
                            break;
                        case TreeViewNodeTypes.AnalysisNode:
                            this._actualLvl = TreeViewNodeTypes.AnalysisNode;
                            this.labelPosition.Text = String.Empty;
                            break;
                        case TreeViewNodeTypes.GeographyNode:
                            this._actualLvl = TreeViewNodeTypes.GeographyNode;
                            this.labelPosition.Text = String.Empty;
                            break;
                        case TreeViewNodeTypes.LocalisationNode:
                            this._actualLvl = TreeViewNodeTypes.LocalisationNode;
                            this.labelPosition.Text = String.Empty;
                            break;
                        case TreeViewNodeTypes.AgentNode:
                            this._actualLvl = TreeViewNodeTypes.AgentNode;
                            this.labelPosition.Text = String.Empty;
                            break;
                        case TreeViewNodeTypes.SitePropertyNode:
                            this._actualLvl = TreeViewNodeTypes.SitePropertyNode;
                            this.labelPosition.Text = String.Empty;
                            break;
                        default:
                            this._actualLvl = TreeViewNodeTypes.Unknown;
                            break;

                    }
                }
                catch (ConnectionCorruptedException ex)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    pictureBoxHome_Click(null, null);
                    return;
                }

                Cursor.Current = Cursors.Default;
                enableDisableToolbarButtons(_actualLvl);
                enableDisableButtons(_actualLvl);
            }
        }
        /// <summary>
        /// Handles the Click event of the pictureBoxNewIdentificationUnit control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void pictureBoxNewIdentificationUnit_Click(object sender, EventArgs e)
        {
            if (this.treeViewFieldData.Nodes.Count == 0 || this.treeViewFieldData.SelectedNode == null)
            {
                return;
            }
            Cursor.Current = Cursors.WaitCursor;
            TreeNode parentNode = this.treeViewFieldData.SelectedNode;
            NewIdentificationUnitDialog dlg;
            TreeViewNodeData data = this.treeViewFieldData.SelectedNode.Tag as TreeViewNodeData;

            if (data == null)
                return;

            // if parent Node is also IdentificationUnit then set some default data
            int? seriesID = null;
            CollectionEvent ev = null;
            CollectionSpecimen cs = null;
            IdentificationUnit parentIU = null;
            if (data.NodeType == TreeViewNodeTypes.IdentificationUnitNode)
            {
                try
                {
                    parentIU = DataFunctions.Instance.RetrieveIdentificationUnit((int)data.ID);
                }
                catch (ConnectionCorruptedException ex)
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Data of parent Identification Unit couldn't be retrieved. ("+ex.Message+")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    return;
                }

                if (parentIU != null)
                {
                    cs = parentIU.CollectionSpecimen;
                    ev = cs.CollectionEvent;
                    seriesID = ev.SeriesID;
                    try
                    {
                        if (seriesID != null)
                        {
                            EventSeriess.Instance.Find((int)seriesID);
                            this._events = EventSeriess.Instance.CollectionEvents;

                        }
                        else
                            this._events = new CollectionEvents(null);
                    }
                    catch (ConnectionCorruptedException ex)
                    {
                        MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        return;
                    }
                    this._events.Find((int)ev.CollectionEventID);
                    this._specimen = this._events.Specimen;
                    this._specimen.Find((int)cs.CollectionSpecimenID);
                    this._iu = this._specimen.IdentificationUnits;
                    try
                    {
                        dlg = new NewIdentificationUnitDialog(cs, parentIU, this._iu);
                    }
                    catch (ContextCorruptedException ex)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show(ex.Message, "Context Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        return;
                    }
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Data of parent Identification Unit couldn't be retrieved.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    return;
                }
            }
            else if (data.NodeType == TreeViewNodeTypes.SpecimenNode)
            {
                try
                {
                    TreeNode eventNode = parentNode.Parent;
                    TreeViewNodeData eventData = eventNode.Tag as TreeViewNodeData;
                    TreeNode seriesNode = eventNode.Parent;
                    TreeViewNodeData seriesData = seriesNode.Tag as TreeViewNodeData;
                    seriesID = seriesData.ID;

                    if (seriesID != null)
                    {
                        EventSeriess.Instance.Find((int)seriesID);
                        this._events = EventSeriess.Instance.CollectionEvents;

                    }
                    else
                        this._events = new CollectionEvents(null);
                    this._events.Find((int)eventData.ID);
                    ev = this._events.Current;
                    this._specimen = this._events.Specimen;
                }
                catch (ConnectionCorruptedException ex)
                {
                    MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    return;
                }

                if (this._specimen.Find((int)data.ID))
                {
                    cs = this._specimen.Current;
                    this._iu = this._specimen.IdentificationUnits;
                    try
                    {
                        dlg = new NewIdentificationUnitDialog(cs, this._iu);
                    }
                    catch (ConnectionCorruptedException ex)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("Associated IdentificationUnits with selected Specimen couldn't be retrieved. (" + ex.Message + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        return;
                    }
                    catch (ContextCorruptedException ex)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("Associated IdentificationUnits with selected Specimen couldn't be retrieved. (" + ex.Message + ")", "Context Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        return;
                    }
                }
                else
                {
                    Cursor.Current = Cursors.Default;
                    MessageBox.Show("Data of selected Specimen couldn't be retrieved.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    return;
                }
            }
            else
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show("Wrong node selected.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return;
            }

            // center dialog
            dlg.Location = new Point((this.Size.Width) / 2 - (dlg.Size.Width) / 2, this.Location.Y);
            Cursor.Current = Cursors.Default;
            dlg.ShowDialog();
            moveTaxonomicGroupToFront(dlg.TaxonomicGroup);
            if (dlg.Current != null)
            {
                IdentificationUnit iu = dlg.Current;
                TreeViewNodeData parentData = parentNode.Tag as TreeViewNodeData;
                this.treeViewFieldData.BeginUpdate();
                treeViewFieldData.SuspendLayout();
                if (parentData.NodeType == TreeViewNodeTypes.SpecimenNode)
                {
                    displaySpecimen(cs);
                }
                if (parentData.NodeType == TreeViewNodeTypes.IdentificationUnitNode)
                {
                    displayIdentificationUnit(parentIU);
                }
                foreach (TreeNode node in treeViewFieldData.SelectedNode.Nodes)
                {
                    TreeViewNodeData nodeData = node.Tag as TreeViewNodeData;
                    if (nodeData.NodeType == TreeViewNodeTypes.IdentificationUnitNode && nodeData.ID == iu.IdentificationUnitID)
                    {
                        this.treeViewFieldData.SelectedNode = node;
                        break;
                    }
                }

                if (!this.treeViewFieldData.SelectedNode.IsExpanded && this._tvOperations.expandLvl <= ExpandLevel.IdentificationUnit)
                {
                    this._expandTrigger = false;
                    this.treeViewFieldData.SelectedNode.ExpandAll();
                    this._expandTrigger = true;
                }

                this.treeViewFieldData.SelectedNode.EnsureVisible();
                this._actualLvl = TreeViewNodeTypes.IdentificationUnitNode;
                this.enableDisableToolbarButtons(_actualLvl);
                this.enableDisableButtons(_actualLvl);
                treeViewFieldData.ResumeLayout();
                this.treeViewFieldData.EndUpdate();
            }
        }
        private void pictureBoxNewEvent_Click(object sender, EventArgs e)
        {
            if (this.treeViewFieldData.SelectedNode == null || this.treeViewFieldData.SelectedNode.Tag == null)
            {
                MessageBox.Show("Error, EventSeriesNode has to be selected");
                return;
            }

            TreeNode seriesNode = this.treeViewFieldData.SelectedNode;
            TreeViewNodeData data = this.treeViewFieldData.SelectedNode.Tag as TreeViewNodeData;

            if (data.NodeType != TreeViewNodeTypes.EventSeriesNode)
            {
                MessageBox.Show("Error, EventSeriesNode has to be selected!");
                return;
            }
            int? seriesID = data.ID;
            try
            {
                if (data.ID != null)
                {
                    EventSeriess.Instance.Find((int) data.ID);
                    this._events = EventSeriess.Instance.CollectionEvents;
                }
                else
                    this._events = new CollectionEvents(null);
            }
            catch (ConnectionCorruptedException ex)
            {
                MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return;
            }

            NewEventDialog dlg = null;
            try
            {
                dlg = new NewEventDialog(true);
                if(dlg == null)
                    return;
            }
            catch (ContextCorruptedException ex)
            {
                Cursor.Current = Cursors.Default;
                MessageBox.Show(ex.Message, "Context Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                return;
            }

            dlg.Location = new Point((this.Size.Width) / 2 - (dlg.Size.Width) / 2, this.Location.Y);
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                Cursor.Current = Cursors.WaitCursor;
                if (this._events != null)
                {
                    try
                    {
                        if (StaticGPS.isOpened())
                        {
                            if (StaticGPS.position != null)
                            {
                                double altitude = StaticGPS.position.SeaLevelAltitude;
                                double latitude = float.Parse(StaticGPS.position.Latitude.ToString());
                                double longitude = float.Parse(StaticGPS.position.Longitude.ToString());
                                this._events.CreateNewEvent(seriesID, altitude, longitude, latitude, StaticGPS.position.SatelliteCount, StaticGPS.position.PositionDilutionOfPrecision);
                            }
                            else
                            {
                                this._events.CreateNewEvent(seriesID);
                            }
                        }
                        else
                        {
                            this._events.CreateNewEvent(seriesID);
                        }
                    }
                    catch (ConnectionCorruptedException ex)
                    {
                        MessageBox.Show("New CollectionEvent couldn't be created. ("+ex.Message+")", "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        return;
                    }
                    catch (DataFunctionsException ex)
                    {
                        MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                        return;
                    }

                    if (dlg.EventNumber != null)
                        this._events.Current.CollectorsEventNumber = dlg.EventNumber;

                    if (dlg.Notes != null)
                        this._events.Current.Notes = dlg.Notes;

                    if (dlg.DateSupplement != null)
                        this._events.Current.CollectionDateSupplement = dlg.DateSupplement;

                    try
                    {
                        DataFunctions.Instance.Update(this._events.Current);
                    }
                    catch (ConnectionCorruptedException ex)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("Collection Event Data (Event Number, Notes, Date Supplement) couldn't be saved. (" + ex.Message + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }
                    catch (DataFunctionsException ex)
                    {
                        Cursor.Current = Cursors.Default;
                        MessageBox.Show("Collection Event Data (Event Number, Notes, Date Supplement) couldn't be saved. ("+ex.Message+")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
                    }

                    TreeNode evNode = new TreeNode();
                    this.treeViewFieldData.BeginUpdate();
                    this._tvOperations.parametrizeEventNode(this._events.Current, evNode);
                    this.treeViewFieldData.SelectedNode.Nodes.Add(evNode);
                    this.treeViewFieldData.SelectedNode = evNode;
                    if (!this.treeViewFieldData.SelectedNode.IsExpanded)
                    {
                        this._expandTrigger = false;
                        this.treeViewFieldData.SelectedNode.ExpandAll();
                        this._expandTrigger = true;
                    }

                    // create automatically CollectionSpecimen
                    CollectionSpecimen spec = null;
                    try
                    {
                        this._specimen = this._events.Specimen;
                        spec = this._specimen.CreateSpecimen();
                        this._iu = this._specimen.IdentificationUnits;
                    }
                    catch (Exception ex)
                    {
                        String errorMsg = "";
                        Cursor.Current = Cursors.Default;
                        if (spec != null)
                        {
                            try
                            {
                                DataFunctions.Instance.Remove(spec);
                            }
                            catch (ConnectionCorruptedException except)
                            {
                                errorMsg = "Specimen couldn't be automatically assigned to new Collection Event. (" + except.Message + ")";
                            }
                        }

                        if (errorMsg.Equals(String.Empty))
                        {
                            errorMsg = "Specimen couldn't be automatically assigned to new Collection Event. (" + ex.Message + ")";
                        }
                        MessageBox.Show(errorMsg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

                        this.treeViewFieldData.EndUpdate();
                        return;
                    }

                    TreeNode specNode = new TreeNode();
                    this._tvOperations.parametrizeOnlySpecimenNode(spec, specNode);
                    this.labelPosition.Text = this._specimen.Position;
                    this.treeViewFieldData.SelectedNode.Nodes.Add(specNode);
                    this.treeViewFieldData.SelectedNode = specNode;
                    if (!this.treeViewFieldData.SelectedNode.IsExpanded)
                    {
                        this._expandTrigger = false;
                        this.treeViewFieldData.SelectedNode.ExpandAll();
                        this._expandTrigger = true;
                    }
                    this.enableDisableButtons(TreeViewNodeTypes.SpecimenNode);
                    this._actualLvl = TreeViewNodeTypes.SpecimenNode;
                    enableDisableToolbarButtons(_actualLvl);
                    this.treeViewFieldData.EndUpdate();
                }
            }
            Cursor.Current = Cursors.Default;
        }
 private void pictureBoxHome_Click(object sender, EventArgs e)
 {
     Cursor.Current = Cursors.WaitCursor;
     try
     {
         EventSeriess.Instance.Current = null;
         this._events = null;
         this._specimen = null;
         this._iu = null;
         //this.resetTreeView(); Anzeige wird nicht aktualisert. Warum?
         this.displayAllEventSeries();
         this.labelPosition.Text = String.Empty;
         this.enableDisableButtons(TreeViewNodeTypes.Root);
         this._actualLvl = TreeViewNodeTypes.Root;
         this.enableDisableToolbarButtons(_actualLvl);
     }
     catch (ConnectionCorruptedException ex)
     {
         Cursor.Current = Cursors.Default;
         MessageBox.Show("Severe Program Exception (" + ex.Message + "). Program will be closed.", "Severe Exception", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
         this.Close();
     }
     Cursor.Current = Cursors.Default;
 }