August 19, 2006

Useful Object Oriented Programming Links

Useful Javascript Links

Javascript tit-bit:
You can always include the last update date on your page by using the following code:
<script language="JavaScript">
document.write("This page created by XXXXX Last update:" + document.lastModified);

BeginInvoke On A Gui Object

Good article on BeginInvoke and Invoke differences

On the GUI class (form or control derived object):

  private delegate void DataChanged(Object Sender, BindingEventArgs Args);

  private void HandleDataChanged(Object
Sender, BindingEventArgs Args)
    // If this delegate is invoked on another thread
    if (InvokeRequired)
      // Asynchronously call THIS method later
      // but this time on our own GUI thread
      // BeginInvoke translates to a PostMessage call on
      // our window.
      BeginInvoke(new DataChangedDelegate(HandleDataChanged),
 new object[] { Sender, Args });
    else // when the method is invoked on the GUI thread
    { // Update our forms/controls as required

WIndows CE Notes.Links

Initializing A ComboBox In CE.NET With The Values of An Enum

  SomeEnum status;
  int offset = 0;
  bool defined = false;
      status = (SomeEnum)offset++;
      defined = Enum.IsDefined(typeof(SomeEnum), status.ToString());
      if (defined)

  } while (IsDefined);
  comboBox.SelectedIndex = 0;

Using A ListView

To get a nice excel type view of the list set the properties 'View=Details' and 'GridLines=True' or put the following code in the designer

//this.listView.Anchor =  //Dont forget to set the anchors appropriately
this.listView.AllowColumnReorder = true;
this.listView.View = System.Windows.Forms.View.Details; 
this.listView.GridLines = true;

// Select highlights a full row
this.listView.FullRowSelect = true;

// Select a single row only
this.listView.MultiSelect = false;

// Keep selected rows highlited even when the control does not have the focus 
this.listView.HideDelection = false; 
Adding items to the ListView
private const int NAME_COLUMN = 0;
private const int ID_COLUMN = 1;

void PopulateListView()
    // DONT call "listView.Clear()" or you'll lose all your
    // column data and then wonder why you table is empty!
    // Add the item to the ListView
    string[] fields = new string[] 
    fields[NAME_COLUMN] = xxx.Name;
    fields[ID_COLUMN] = xxx.Id.ToString();

    ListViewItem lvItem = new ListViewItem(fields);
    // Insert at the top of the listview control
    listView.Items.Insert(0, lvItem);
    // Append to the end of the ListView control

    // with custom listviewitems
    MyListViewItem li = new MyListViewItem(...)

To update a column:
private void UpdateIdColumn(int id)
    listView.Items[m_SelectedIndex].SubItems[ID_COLUMN].Text = id.ToString();
Optionally define a custom list view item which holds a reference to one of your types XXX
internal class MyListViewItem : ListViewItem
    internal MyListViewItem(XXX xxx)
        : base(new string[] { 
        XXX = xxx;

    internal XXX XXX { get; set; }
private void listView_SelectedIndexChanged(object sender, EventArgs e)
    if (listView.SelectedItems.Count == 0)
 // Are there any controls need enabling/
 // disabling when the nothing is selected
        button.Enabled = false;
    // Are there any controls need enabling/
    // disabling when the nothing is selected
    otherCtrl.Enabled = true;
    foreach (ListViewItem lvi in lvParts.SelectedItems)
 // or
        MyCustomListItem li = lvi as MyCustomListItem;
        if (li != null)

Dont forget to add a method to resize the columns:
private void FitColumnsToListView()
    int extra = ListViewSpareColumnSpace(lvParts);
    if (extra > 0)
        lvParts.Columns[0].Width += extra;

private int ListViewSpareColumnSpace(ListView lv)
    if (lv.Columns.Count == 0)
        return 0;

    int width = lv.Size.Width;
    int usedWidth = 0;
    // 2 pixels for the divider lines
    foreach (ColumnHeader ch in lv.Columns)
        usedWidth += ch.Width + 2;
    int extra = width - usedWidth;
    return extra;
To disable selection of listview items, handle the ItemSelectionChanged event on the listView:
private void listView_ItemSelectionChanged(object sender, 
  ListViewItemSelectionChangedEventArgs e)
  if (e.IsSelected)
    e.Item.Selected = false;

Use Reflection To Set A Value On A Field

Setting a field on an object by reflection private static void SetFieldViaReflection( object obj, string fieldName, object value) { Type InstanceType = Instance.GetType(); FieldInfo fi = InstanceType.GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Instance); if (fi != null) { fi.SetValue(obj, value); } } SetFieldByReflection(object, "m_field", somevalue as object);