HeaderPane
Main
Hide
Share Link
Print

EventViewer Sample (C#)

This sample logs the events of two fields and a calendar. The appointment field refuses Saturdays and Sundays in its ValueValidating event, and the due date field accepts "+7" or "-3" as a number of days in its TextParsing event.

The source code is located at C:\Program Files (x86)\Softelvdm\SftDate NET 3.0\Samples\CSharp\EventViewer\Form1.cs.

using System;
using System.ComponentModel;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using Softelvdm.SftDateNET;

namespace EventViewer {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private string strLastEvent = "";
        private bool fLastEventDup = false;

        // This is a small helper routine to show all properties and fields of an object
        private void DumpValues(string title, object o) {

            string s = "";

            if (strLastEvent == title) {
                if (!fLastEventDup) {
                    s = "Additional " + title + " events ... now suppressed";
                    fLastEventDup = true;
                } else
                    return;
            } else {
                strLastEvent = title;
                fLastEventDup = false;

                PropertyInfo[] api = o.GetType().GetProperties();
                foreach (PropertyInfo pi in api)
                    s += pi.Name + "=" + Describe(pi.GetValue(o, new object[] { })) + " ";
                FieldInfo[] afi = o.GetType().GetFields();
                foreach (FieldInfo fi in afi)
                    s += fi.Name + "=" + Describe(fi.GetValue(o)) + " ";
            }

            ListViewItem item = listView1.Items.Add(new ListViewItem(new string[] { title, s }));
            listView1.SelectedItems.Clear();
            item.Selected = true;
            item.EnsureVisible();
        }

        private string Describe(object value) {
            return value == null ? "null" : value.ToString();
        }

        private void buttonClear_Click(object sender, EventArgs e) {
            listView1.Items.Clear();
            strLastEvent = "";
            fLastEventDup = false;
        }

        // sftDate1 - a date and time field; appointments are only accepted on weekdays

        private void sftDate1_ValueValidating(object sender, ValueValidatingEventArgs e) {
            if (e.NewValue.HasValue && (e.NewValue.Value.DayOfWeek == DayOfWeek.Saturday || e.NewValue.Value.DayOfWeek == DayOfWeek.Sunday)) {
                e.Cancel = true;
                DumpValues("sftDate1.ValueValidating (canceled, a weekend)", e);
            } else
                DumpValues("sftDate1.ValueValidating", e);
        }

        private void sftDate1_ValueChanged(object sender, EventArgs e) {
            DumpValues("sftDate1.ValueChanged (" + sftDate1.Text + ")", e);
        }

        private void sftDate1_DropDownOpening(object sender, CancelEventArgs e) {
            DumpValues("sftDate1.DropDownOpening", e);

            // e.Cancel = true; would keep the dropdown closed
        }

        private void sftDate1_DropDownOpened(object sender, EventArgs e) {
            DumpValues("sftDate1.DropDownOpened", e);
        }

        private void sftDate1_DropDownClosed(object sender, EventArgs e) {
            DumpValues("sftDate1.DropDownClosed", e);
        }

        private void sftDate1_Enter(object sender, EventArgs e) {
            DumpValues("sftDate1.Enter", e);
        }

        private void sftDate1_Leave(object sender, EventArgs e) {
            DumpValues("sftDate1.Leave", e);
        }

        private void sftDate1_KeyDown(object sender, KeyEventArgs e) {
            DumpValues("sftDate1.KeyDown", e);
        }

        // sftDate2 - a free-form field; besides dates in the culture's format it accepts +n and -n days from today

        private void sftDate2_TextParsing(object sender, TextParsingEventArgs e) {
            Match m = Regex.Match(e.Text.Trim(), @"^([+-])\s*(\d{1,4})$");
            if (m.Success) {
                int days = int.Parse(m.Groups[2].Value);
                e.Value = DateTime.Today.AddDays(m.Groups[1].Value == "-" ? -days : days);
                e.Handled = true;
            }
            DumpValues("sftDate2.TextParsing", e);
        }

        private void sftDate2_ValueValidating(object sender, ValueValidatingEventArgs e) {
            DumpValues("sftDate2.ValueValidating", e);
        }

        private void sftDate2_ValueChanged(object sender, EventArgs e) {
            DumpValues("sftDate2.ValueChanged (" + sftDate2.Text + ")", e);
        }

        private void sftDate2_DropDownOpened(object sender, EventArgs e) {
            DumpValues("sftDate2.DropDownOpened", e);
        }

        private void sftDate2_DropDownClosed(object sender, EventArgs e) {
            DumpValues("sftDate2.DropDownClosed", e);
        }

        private void sftDate2_Enter(object sender, EventArgs e) {
            DumpValues("sftDate2.Enter", e);
        }

        private void sftDate2_Leave(object sender, EventArgs e) {
            DumpValues("sftDate2.Leave", e);
        }

        // sftCalendar1

        private void sftCalendar1_ValueChanged(object sender, EventArgs e) {
            DumpValues("sftCalendar1.ValueChanged (" + sftCalendar1.Value + ")", e);
        }

        private void sftCalendar1_RangeChanged(object sender, EventArgs e) {
            DumpValues("sftCalendar1.RangeChanged", e);
        }

        private void sftCalendar1_DateActivated(object sender, DateEventArgs e) {
            DumpValues("sftCalendar1.DateActivated", e);
        }

        private void sftCalendar1_ViewChanged(object sender, EventArgs e) {
            DumpValues("sftCalendar1.ViewChanged (" + sftCalendar1.View + ")", e);
        }

        private void sftCalendar1_DisplayChanged(object sender, EventArgs e) {
            DumpValues("sftCalendar1.DisplayChanged (" + sftCalendar1.DisplayStart.ToShortDateString() + ")", e);
        }

        private void sftCalendar1_TodayClicked(object sender, EventArgs e) {
            DumpValues("sftCalendar1.TodayClicked", e);
        }

        private void sftCalendar1_ClearClicked(object sender, EventArgs e) {
            DumpValues("sftCalendar1.ClearClicked", e);
        }

        private void sftCalendar1_DayFormatting(object sender, DayFormattingEventArgs e) {
            // DumpValues("sftCalendar1.DayFormatting", e);  // deliberately suppressed, it occurs for every day shown
        }

        private void sftCalendar1_Enter(object sender, EventArgs e) {
            DumpValues("sftCalendar1.Enter", e);
        }

        private void sftCalendar1_Leave(object sender, EventArgs e) {
            DumpValues("sftCalendar1.Leave", e);
        }

        private void sftCalendar1_KeyDown(object sender, KeyEventArgs e) {
            DumpValues("sftCalendar1.KeyDown", e);
        }

        private void sftCalendar1_MouseDown(object sender, MouseEventArgs e) {
            DumpValues("sftCalendar1.MouseDown", e);
        }
    }
}