HeaderPane
Main
Hide
Share Link
Print

EventViewer Sample (C#)

This sample logs the events of the SftStyle component, of a slider and a range slider, of a task dialog page with radio buttons, a verification check box, an expander, a link, an Apply button that keeps the dialog open and a Help button, and of the color dialog with its Help button. While the Leave the event list as it is check box is set, the Styling event handler leaves the event list as it is in dark mode. On the .NET Framework the Scaling event is logged when a form moves to a monitor with another scale factor.

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

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

using Softelvdm.SftStyleNET;
using Softelvdm.SftStyleNET.Controls;

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) {
            if (value == null)
                return "null";
            Control c = value as Control;
            return c != null ? c.GetType().Name + " " + c.Name : value.ToString();
        }

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

        // sftStyle1 - the mode of every form of the application; the check box switches it

        private void checkDark_CheckedChanged(object sender, EventArgs e) {
            DarkModeEnum mode = checkDark.Checked ? DarkModeEnum.On : DarkModeEnum.Off;
            sftStyle1.DarkMode = mode;
            sftSlider1.DarkMode = mode;
            sftSlider2.DarkMode = mode;
        }

        private void sftStyle1_DarkModeChanged(object sender, EventArgs e) {
            DumpValues("sftStyle1.DarkModeChanged (" + (sftStyle1.DarkModeActive ? "dark" : "light") + ")", e);
        }

        private void sftStyle1_HighContrastChanged(object sender, EventArgs e) {
            DumpValues("sftStyle1.HighContrastChanged (" + (sftStyle1.HighContrastActive ? "on" : "off") + ")", e);
        }

        // Occurs for each control before it is styled; the event list can be left as it is.
        private void sftStyle1_Styling(object sender, StylingEventArgs e) {
            e.Cancel = checkLeaveList.Checked && e.Control == listView1;
            DumpValues("sftStyle1.Styling" + (e.Cancel ? " (left as it is)" : ""), e);
        }

        // Occurs on .NET Framework before a form or a control is scaled for the DPI of its monitor.
        private void sftStyle1_Scaling(object sender, ScalingEventArgs e) {
            DumpValues("sftStyle1.Scaling", e);
        }

        // sftSlider1 - a value; sftSlider2 - a range with two thumbs

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

        private void sftSlider1_Scroll(object sender, EventArgs e) {
            DumpValues("sftSlider1.Scroll", e);
        }

        private void sftSlider2_RangeChanged(object sender, EventArgs e) {
            DumpValues("sftSlider2.RangeChanged (" + sftSlider2.LowerValue + " to " + sftSlider2.UpperValue + ")", e);
        }

        private void sftSlider2_ValueChanged(object sender, EventArgs e) {
            DumpValues("sftSlider2.ValueChanged (" + sftSlider2.LowerValue + ")", e);
        }

        private void sftSlider2_Scroll(object sender, EventArgs e) {
            DumpValues("sftSlider2.Scroll", e);
        }

        // A task dialog page with every kind of element; Apply keeps the dialog open.
        private void buttonTaskDialog_Click(object sender, EventArgs e) {
            SftTaskDialogPage page = new SftTaskDialogPage {
                Caption = "Export",
                Heading = "Choose the export format",
                Text = "The report covers 1,284 orders. Read the <a href=\"formats\">format notes</a> first.",
                EnableLinks = true,
                Verification = new SftTaskDialogVerificationCheckBox("&Open the file after the export"),
                Expander = new SftTaskDialogExpander("Comma-separated files open in any spreadsheet program; PDF keeps the layout of the report."),
            };
            page.RadioButtons.Add("&Comma-separated values (.csv)").Checked = true;
            page.RadioButtons.Add("&PDF document");
            SftTaskDialogButton apply = new SftTaskDialogButton("&Apply") { AllowCloseDialog = false };
            page.Buttons.Add(apply);
            page.Buttons.Add(SftTaskDialogButton.OK);
            page.Buttons.Add(SftTaskDialogButton.Cancel);
            page.Buttons.Add(SftTaskDialogButton.Help);

            page.Created += (s, a) => DumpValues("page.Created", a);
            page.Destroyed += (s, a) => DumpValues("page.Destroyed", a);
            page.HelpRequest += (s, a) => DumpValues("page.HelpRequest", a);
            page.LinkClicked += (s, a) => DumpValues("page.LinkClicked", a);
            apply.Click += (s, a) => DumpValues("Apply.Click (the dialog stays open)", a);
            foreach (SftTaskDialogRadioButton radio in page.RadioButtons) {
                SftTaskDialogRadioButton r = radio;
                r.CheckedChanged += (s, a) => DumpValues("RadioButton.CheckedChanged (" + r.Text.Replace("&", "") + (r.Checked ? ", checked" : "") + ")", a);
            }
            page.Verification.CheckedChanged += (s, a) => DumpValues("Verification.CheckedChanged (" + page.Verification.Checked + ")", a);
            page.Expander.ExpandedChanged += (s, a) => DumpValues("Expander.ExpandedChanged (" + page.Expander.Expanded + ")", a);

            SftTaskDialogButton clicked = SftTaskDialog.ShowDialog(this, page);
            DumpValues("SftTaskDialog.ShowDialog (" + clicked.Text.Replace("&", "") + ")", EventArgs.Empty);
        }

        private void buttonColorDialog_Click(object sender, EventArgs e) {
            DialogResult result = sftColorDialog1.ShowDialog(this);
            DumpValues("sftColorDialog1.ShowDialog (" + result + ", " + sftColorDialog1.Color.Name + ")", EventArgs.Empty);
        }

        private void sftColorDialog1_HelpRequest(object sender, EventArgs e) {
            DumpValues("sftColorDialog1.HelpRequest", e);
        }
    }
}