Scheduler for WinForms | ComponentOne
Walkthroughs / Customize Appointment Form
In This Topic
    Customize Appointment Form
    In This Topic

    Appointments can be created using the Scheduler for WinForms. This walkthrough guides you on how to customize the existing appointment form. For example, here we demonstrate how to add a custom button and uncheck the reminder checkbox by overriding the existing appointment form.

    1. Define Temporary Variable

      Define temporary variables to determine whether an appointment form is being used. The following code checks if an appointment form is used to create or modify an appointment.

      C#
      Copy Code
      // Flag to determine whether the appointment box is used
      // for creating or modifying an appointment
      bool newEditingStatus = false;
      
      // Refers to the appointment being modified
      C1.Schedule.Appointment currentAppointment;
      
      // Refers to the user defined Appointment Form
      C1.Win.Schedule.Forms.AppointmentForm _customAppointmentForm;
      
    2. Create New Appointment Form

      Observe that the existing Appointment form is a Windows Form. Therefore, to customize or update the existing appointment form, we can implement the code below through either the BeforeAppointmentCreate or BeforeAppointmentShow events. The below code illustrates implementing the new Appointment form using the BeforeAppointmentCreate event.

      C#
      Copy Code
      private void c1Schedule1_BeforeAppointmentCreate(object sender, C1.Schedule.CancelAppointmentEventArgs e)
      {
          e.Cancel = true;
      
          currentAppointment = new C1.Schedule.Appointment();
          currentAppointment.Start = this.c1Schedule1.SelectedInterval.Start;
          currentAppointment.End = this.c1Schedule1.SelectedInterval.End;
          this.c1Schedule1.DataStorage.AppointmentStorage.Appointments.Add(currentAppointment);
      
          _customAppointmentForm = new C1.Win.Schedule.Forms.AppointmentForm(this.c1Schedule1, currentAppointment);
          _customAppointmentForm.FormClosing += CustomAppointmentForm_FormClosing;
      
          // It defines whether the current Appointment box
          // will open for new Appointment or modifying an appointment
          newEditingStatus = true;
      
          ModifyForm(_customAppointmentForm);
      
          _customAppointmentForm.ShowDialog(this);
      }
      

      The below code shows creating the new Appointment form using BeforeAppointmentShow event.

      C#
      Copy Code
      private void c1Schedule1_BeforeAppointmentShow(object sender, CancelAppointmentEventArgs e)
      {
          e.Cancel = true;
      
          this.c1Schedule1.DataStorage.AppointmentStorage.Appointments.Add(e.Appointment);
      
          _customAppointmentForm = new C1.Win.Schedule.Forms.AppointmentForm(this.c1Schedule1, e.Appointment);
          _customAppointmentForm.FormClosing += CustomAppointmentForm_FormClosing;
      
          ModifyForm(_customAppointmentForm);
      
          _customAppointmentForm.ShowDialog(this);
      }
      
    3. Modify Appointment Form

      The next step defines the method to modify the appointment form. The code below checks the number of controls present on the form and adds a new button to the list of controls available on the form.

      C#
      Copy Code
      public void ModifyForm(C1.Win.Schedule.Forms.AppointmentForm _appform)
      {
          // Loop through the controls to do the modifications
          for (int i = _appform.Controls.Count - 1; i >= 0; i--)
          {
              Control c = _appform.Controls[i];
              if (c.GetType() == typeof(Panel))
              {
                  for (int j = c.Controls.Count - 1; j >= 0; j--)
                  {
                      Control d = c.Controls[j];
                      if (d.GetType() == typeof(CheckBox))
                      {
                          // Uncheck the reminder check box while
                          // creating a new appointment
                          if (d.Name == "chkReminder")
                          {
                              if (newEditingStatus == true)
                              {
                                  ((CheckBox)d).Checked = false;
                              }
                          }
                      }
                  }
              }
              else if (c.GetType() == typeof(ToolStrip))
              {
                  // Add a new custom button
                  if (c.Name.Equals("toolStrip1"))
                  {
                      ToolStripButton btn = new ToolStripButton();
                      btn.Text = "Custom Button";
                      btn.Click += CustomButton_Click;
                      ((ToolStrip)c).Items.Add(btn);
                  }
      
                  for (int j = 0; j < ((ToolStrip)c).Items.Count; j++)
                  {
                      ToolStripItem d = ((ToolStrip)c).Items[j];
                      if (d.GetType() == typeof(ToolStripButton))
                      {
                          if (d.Name == "btnSave")
                          {
                              ((ToolStripButton)d).Click += SaveButton_Click;
                          }
                      }
                  }
              }
          }
      }
      
    4. Add Event Handler for Newly Created Custom Button

      For the custom button that was added in the previous step, we will add a handler event. The code below displays a message box when the new custom button is clicked.

      C#
      Copy Code
      private void CustomButton_Click(object sender, EventArgs e)
      {
          if (currentAppointment != null)
          {
              MessageBox.Show("This is a new button Action box");
          }
      }
      
    5. Save the Appointment

      You can save the appointment created through the save button available on the Appointment form using the code below.

      C#
      Copy Code
      private void SaveButton_Click(object sender, EventArgs e)
      {
          ToolStripButton button = (ToolStripButton)sender;
          Appointment app = ((C1.Win.Schedule.Forms.AppointmentForm)button.Owner.FindForm()).Appointment;
          c1Schedule1.DataStorage.AppointmentStorage.Appointments.Add(app);
      }
      
    6. Reset Appearance of New Appointment Form on Close

      You can also reset the appearance of the new Appointment form by resetting the temporary variable (flag value) set in the first step of this walkthrough as illustrated in the code below.

      C#
      Copy Code
      public void CustomAppointmentForm_FormClosing(object sender, FormClosingEventArgs e)
      {
          newEditingStatus = false;
      }