HeaderPane
Main
Hide
Share Link
Print

EventViewer Sample (C#)

This sample logs the events of a DropDown and a DropDownList combo box. While the check box is set, its DropDownOpening event handler cancels the dropdowns.

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

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

using Softelvdm.SftBoxNET;

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;
        }

        // The text of the selected item, as the combo box shows it
        private string Selection(SftBox box) {
            return box.SelectedItem == null ? "none" : box.DisplayText(box.SelectedItem);
        }

        // sftBox1 - an edit field with a dropdown; typing completes the name of a fruit

        private void sftBox1_SelectedItemChanged(object sender, EventArgs e) {
            DumpValues("sftBox1.SelectedItemChanged (" + Selection(sftBox1) + ")", e);
        }

        private void sftBox1_SelectionCommitted(object sender, EventArgs e) {
            DumpValues("sftBox1.SelectionCommitted (" + Selection(sftBox1) + ")", e);
        }

        private void sftBox1_SelectedValueChanged(object sender, EventArgs e) {
            DumpValues("sftBox1.SelectedValueChanged", e);
        }

        private void sftBox1_EditTextChanged(object sender, EventArgs e) {
            DumpValues("sftBox1.EditTextChanged (" + sftBox1.Text + ")", e);
        }

        private void sftBox1_TextChanged(object sender, EventArgs e) {
            DumpValues("sftBox1.TextChanged (" + sftBox1.Text + ")", e);
        }

        private void sftBox1_DropDownOpening(object sender, CancelEventArgs e) {
            e.Cancel = checkBlock.Checked;
            DumpValues("sftBox1.DropDownOpening" + (e.Cancel ? " (canceled)" : ""), e);
        }

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

        private void sftBox1_DropDownClosed(object sender, DropDownClosedEventArgs e) {
            DumpValues("sftBox1.DropDownClosed", e);
        }

        private void sftBox1_DropDownResized(object sender, EventArgs e) {
            DumpValues("sftBox1.DropDownResized (" + sftBox1.DropDownSize.Width + " x " + sftBox1.DropDownSize.Height + ")", e);
        }

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

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

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

        // sftBox2 - a list of countries with two columns, without an edit field

        private void sftBox2_SelectedItemChanged(object sender, EventArgs e) {
            DumpValues("sftBox2.SelectedItemChanged (" + Selection(sftBox2) + ")", e);
        }

        private void sftBox2_SelectionCommitted(object sender, EventArgs e) {
            DumpValues("sftBox2.SelectionCommitted (" + Selection(sftBox2) + ")", e);
        }

        private void sftBox2_SelectedValueChanged(object sender, EventArgs e) {
            DumpValues("sftBox2.SelectedValueChanged", e);
        }

        private void sftBox2_TextChanged(object sender, EventArgs e) {
            DumpValues("sftBox2.TextChanged (" + sftBox2.Text + ")", e);
        }

        private void sftBox2_DropDownOpening(object sender, CancelEventArgs e) {
            e.Cancel = checkBlock.Checked;
            DumpValues("sftBox2.DropDownOpening" + (e.Cancel ? " (canceled)" : ""), e);
        }

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

        private void sftBox2_DropDownClosed(object sender, DropDownClosedEventArgs e) {
            DumpValues("sftBox2.DropDownClosed", e);
        }

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

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

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