Exemplo n.º 1
0
        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            if (tbTitle.Text == String.Empty)
            {
                Alert("Events must have title!");
            }
            else if (btnSubmit.Text != "Save")
            {
                CalEvent ev;
                // because class has static event, and i want to use not static
                // but static is needed for RecurringForm, therefore copy recurring parameter into
                // new CalEvent object
                if (cbRecurring.Checked)
                {
                    ev = new CalEvent();
                    ev.SetRecurringType(cEvent.GetRecurringType());
                    ev.SetRecurring(true);
                }
                else
                {
                    ev = new CalEvent();
                }
                try
                {
                    //Set all the values for CalEvent
                    ev.SetAppointment(cbAppointment.Checked);
                    ev.SetTitle(tbTitle.Text);

                    StartDateTime = startDate.Value.Date +
                                    startTime.Value.TimeOfDay;

                    if (cbRecurring.Checked)
                    {
                        EndDateTime = endDate.Value.Date +
                                      endTime.Value.TimeOfDay;
                    }
                    else
                    {
                        EndDateTime = startDate.Value.Date +
                                      endTime.Value.TimeOfDay;
                    }


                    ev.SetDate(StartDateTime, EndDateTime);

                    //retrieve contact
                    if (cbAppointment.Checked)
                    {
                        ArrayList contacts = (ArrayList)lbPerson.DataSource;
                        int       index    = contacts.IndexOf(lbPerson.SelectedItem);
                        ev.SetContact(GetContactById(index));
                    }

                    ev.SetLocation(tbLocation.Text);
                    ev.InitNullValues();

                    XmlControl.AddEvent(ev);

                    //initListBox();
                }
                catch (ArgumentNullException ex)
                {
                    MessageBox.Show(ex.ToString() + "\n Button Click -> AddEvent.cs");
                }
                catch (Exception ex) { MessageBox.Show(ex.ToString() + "\n Button Click -> AddEvent.cs"); }

                //lbEvent.DataSource = XmlControl.GetEventsList();
                ArrayList Events     = XmlControl.GetEventsList();
                ArrayList EventsData = new ArrayList();
                for (int i = 0; i < Events.Count; i++)
                {
                    EventsData.Add(((CalEvent)Events[i]).GetDates(StartDateTime, EndDateTime));
                }
                Thread.Sleep(300);
                SetEventsData();
                tbLocation.Text = "";
                tbTitle.Text    = "";

                calendarForm.Repaint();
            }
            else
            {
                ArrayList eList = new ArrayList();
                Thread.Sleep(300);
                eList = SetEventsData();

                //eList[eventEditedId] = new CalEvent();
                CalEvent ev = new CalEvent();
                ev.SetTitle(tbTitle.Text);
                ev.SetDate(StartDateTime, EndDateTime);
                if (cbRecurring.Checked)
                {
                    //try (!recurDatesSet && Object.Equals(recurringDates, default(ArrayList)))
                    if (Object.Equals(cEvent, default(CalEvent)))
                    {
                        ev.SetRecurringType(((CalEvent)eList[EventEditedId]).GetRecurringType());
                    }
                    else
                    {
                        ev.SetRecurringType(cEvent.GetRecurringType());
                    }
                    //catch(Exception ex)
                    //{
                    //    (CalEvent)eList[eventEditedId]).SetRecurringType(cEvent.GetRecurringType());
                    //}

                    ev.SetRecurring(true);
                }
                else
                {
                    ev.SetRecurring(false);
                }
                if (cbAppointment.Checked)
                {
                    ArrayList contacts = (ArrayList)lbPerson.DataSource;
                    int       index    = contacts.IndexOf(lbPerson.SelectedItem);
                    ev.SetContact(GetContactById(index));
                    ev.SetAppointment(true);
                    ev.SetLocation(tbLocation.Text);
                }

                ev.InitNullValues();

                //eList[eventEditedId] = ev;

                ArrayList FullEvents = XmlControl.GetEventsList();
                foreach (CalEvent Event in FullEvents)
                {
                    if (Event.ToString() == ((CalEvent)eList[EventEditedId]).ToString())
                    {
                        FullEvents.Remove(Event);
                        break;
                    }
                }
                //FullEvents.Remove((CalEvent)eList[eventEditedId]);
                FullEvents.Add(ev);
                //((CalEvent)eList[eventEditedId]).SetId();

                //Contact c = new Contact();
                //ContactsListToXml(contactList);
                EventsListToXml(FullEvents);

                //lbEvent.DataSource = eList;
                Thread.Sleep(300);
                SetEventsData();

                lbEvent.Update();

                btnSubmit.Text = "Add New Event!";


                tbLocation.Text = "";
                tbTitle.Text    = "";

                StartDateTime   = DateTime.Now;
                EndDateTime     = DateTime.Now;
                startDate.Value = StartDateTime;
                startTime.Value = StartDateTime;
                endDate.MinDate = new DateTime(1753, 1, 1, 1, 1, 1);
                endDate.Value   = EndDateTime;
                endTime.Value   = EndDateTime;

                StopCBRecurringListener = true;
                cbRecurring.Checked     = false;
                StopCBRecurringListener = false;

                cbAppointment.Checked = false;

                calendarForm.Repaint();
            }
        }