One point with which I want to close off for today: Ownerdraw stuff in C#. Yesterday, I was comparing code to create owner-draw listboxes and comboboxes in both Delphi and C# and noticed how remarkably easy it’s to switch from Delphi to C#: if you did this in Delphi before, the procedure is almost the same in C# (once again this is not surprising if you know who designed C#, earlier at xsamplex)
In Delphi for example, most VCL components have an ‘ownerdraw’ property: switching this one to either true (or to a specific enumerated value like ‘Ownerdrawfixed’), you generally capture two events to draw (and colour) individual items. The same is true for C#: you set the DrawMode property either to ‘OwnerDrawFixed’ or ‘OwnerDrawVariable’. Which one you choose depends on what you plan to do with the listbox.
As mentioned earlier, code to actually draw an item is done in an event: for this C# has the DrawItem event. I’m not going to explain the code, because it should speak for itself.
private void cboDataSources_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index > -1) { TDataSourceType nt = (cboDataSources.Items[e.Index] as TDataSource).SourceType; // Background first! e.DrawBackground(); if (nt == TDataSourceType.System) { // Draw image from image list... imlSources.Draw(e.Graphics, e.Bounds.X, e.Bounds.Y + 1, 0); e.Graphics.DrawString(cboDataSources.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds.X + 18, e.Bounds.Y + 2); } // ... other code removed... // // and then draw the focus rectangle... e.DrawFocusRectangle(); } }
What do you mean, it smells like Delphi?