HeaderPane
Main
Hide
Share Link
Print

EventViewer Sample (C#)

This sample logs the events of an amount with up and down buttons and the popup calculator and of a part number with autocomplete and an ellipsis button. Its CustomValidation event handler refuses amounts over 10,000, and while the check box is set, its CalculatorOpening event handler cancels the calculator.

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

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

using Softelvdm.SftMaskNET;

namespace EventViewer {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            sftMask1.Value = 125.5m;
            sftMask2.AutoComplete.Seed(new string[] { "AB-1001", "AB-1002", "AC-2040", "BX-0815", "BX-0900", "CD-3141" });
        }

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

        // sftMask1 - an amount with up and down buttons; typing +, -, * or / opens the calculator

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

        private void sftMask1_ValueChanged(object sender, EventArgs e) {
            DumpValues("sftMask1.ValueChanged (" + (sftMask1.Value.HasValue ? sftMask1.Value.Value.ToString() : "none") + ")", e);
        }

        private void sftMask1_InputRejected(object sender, InputRejectedEventArgs e) {
            DumpValues("sftMask1.InputRejected", e);
        }

        private void sftMask1_ValidationError(object sender, ValidationErrorEventArgs e) {
            DumpValues("sftMask1.ValidationError", e);
        }

        // Amounts over 10,000 are not accepted.
        private void sftMask1_CustomValidation(object sender, CancelEventArgs e) {
            e.Cancel = sftMask1.Value.HasValue && sftMask1.Value.Value > 10000;
            DumpValues("sftMask1.CustomValidation" + (e.Cancel ? " (over 10,000, refused)" : ""), e);
        }

        private void sftMask1_UpDownPress(object sender, UpDownPressEventArgs e) {
            DumpValues("sftMask1.UpDownPress", e);
        }

        private void sftMask1_CalculatorOpening(object sender, CalculatorOpeningEventArgs e) {
            e.Cancel = checkBlock.Checked;
            DumpValues("sftMask1.CalculatorOpening" + (e.Cancel ? " (canceled)" : ""), e);
        }

        private void sftMask1_DropDownOpened(object sender, DropDownEventArgs e) {
            DumpValues("sftMask1.DropDownOpened", e);
        }

        private void sftMask1_DropDownClosed(object sender, DropDownEventArgs e) {
            DumpValues("sftMask1.DropDownClosed", e);
        }

        private void sftMask1_PrePaste(object sender, PrePasteEventArgs e) {
            DumpValues("sftMask1.PrePaste", e);
        }

        private void sftMask1_InputModeChanged(object sender, EventArgs e) {
            DumpValues("sftMask1.InputModeChanged (" + (sftMask1.Overtype ? "overtype" : "insert") + ")", e);
        }

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

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

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

        // sftMask2 - a part number; typing suggests earlier entries, the ellipsis button raises EllipseClick

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

        private void sftMask2_InputRejected(object sender, InputRejectedEventArgs e) {
            DumpValues("sftMask2.InputRejected", e);
        }

        private void sftMask2_ValidationError(object sender, ValidationErrorEventArgs e) {
            DumpValues("sftMask2.ValidationError", e);
        }

        private void sftMask2_EllipseClick(object sender, EventArgs e) {
            DumpValues("sftMask2.EllipseClick", e);
        }

        private void sftMask2_DropDownOpened(object sender, DropDownEventArgs e) {
            DumpValues("sftMask2.DropDownOpened", e);
        }

        private void sftMask2_DropDownClosed(object sender, DropDownEventArgs e) {
            DumpValues("sftMask2.DropDownClosed", e);
        }

        private void sftMask2_AutoCompleteAccept(object sender, AutoCompleteAcceptEventArgs e) {
            DumpValues("sftMask2.AutoCompleteAccept", e);
        }

        private void sftMask2_PrePaste(object sender, PrePasteEventArgs e) {
            DumpValues("sftMask2.PrePaste", e);
        }

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

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

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