Disclaimer: This code posted "as is", on request by OP.
It is working and very well-tested but I cannot take responsibility for its value for the any purposes.
This is a code fragment from the demo application for my unpublished article "Dynamic Method Dispatcher" and is not related to the main topic of my article.
The only reason I post it in advance is that OP asked for it.
The purpose if this post is to give an idea of code design based on separation of data model from UI, not for explaining each declaration in any level of detail.
using System;
using System.Windows.Forms;
using Time = System.DateTime;
ListViewItem ShowCommon(
Time time, ListView listView,
MessageInfo info,
string customData, bool autoResize) {
ListViewItem item =
new ListViewItem(TestRenderers[(int)CommonColumn.Time](ref time, ref info, customData, true));
for (int index = 0; index < listView.Columns.Count - 1; index++)
item.SubItems.Add(new ListViewItem.ListViewSubItem());
for (
CommonColumn index = (CommonColumn)1;
index < (CommonColumn)(Enum.GetValues(typeof(CommonColumn)).Length - 1);
index++) {
item.SubItems[(int)index].Text =
TestRenderers[(int)index](ref time, ref info, customData, true);
}
item.SubItems[listView.Columns.Count - 1].Text = customData;
listView.Items.Add(item);
if (autoResize)
AutoResize(listView);
listView.EnsureVisible(item.Index);
ShowStatus(ref time, ref info, customData);
return item;
}
This code represents general purpose presentation of a Windows message. It transparently passes the instance of the
ListViewItem
for adding data specific to a specialized ranges of message IDs, like keyboard, mouse, status, etc.
Pay attention that no immediate constants are used (except 0, 1 and null). All data is extracted from some enumeration type definitions and arrays based on those definitions.
On enumerations, please see my series of three articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[
^]
Human-readable Enumeration Meta-data[
^]
Enumeration-based Command Line Utility[
^].
The most relevant is the first article in the series, and a bit of the second one. Even though my demo application (small part shown above) does not use any code from any of these three libraries, some ideas are shared.
—SA