List Databinding Performance With DisplayMembers and ValueMembers

My copy of MSDN magazine arrived last night, and I read the article Practical Tips For Boosting The Performance Of Windows Forms Apps. Good read. Anyway, I was shocked to find out that I have been databinding lists improperly ever since I have been using .Net. I frequently wrote my code like this:

//Bad Code
combobox.DataSource = datatable;
combobox.DisplayMember = "State";
combobox.ValueMember = "Id";

//Good Code
combobox.DisplayMember = "State";
combobox.ValueMember = "Id";
combobox.DataSource = datatable;

Apparenly, order matters very much. In the first example, the combobox binds using the DisplayMember, then rebinds when updated with the ValueMember. In the second example, the binding only happens once.

In our current app, we have two lists that contain thousands of items that need to be bound, so we are binding them during the startup process so the user won't wait when requesting that data. The startup time was reduced by just under 40% by changing the order of the code for binding.

Posted: Wednesday, February 15, 2006 4:36:53 PM (Eastern Standard Time, UTC-05:00)  #    Comments - Trackback
.Net 2.0 | Performance | Winforms

Adding Images to CommandBarButtons in VSTO

An old VSTO 2003 post by Kathleen McGrath about how to add images to a CommandBarButton. It still works in VSTO 2005. The example uses the AxHost object to convert bitmaps to stdole.IPictureDisp types for Office toolbars.
Posted: Monday, February 13, 2006 10:35:30 PM (Eastern Standard Time, UTC-05:00)  #    Comments - Trackback
Office | VSTO

The Clipboard Ring Rules!

Checking amongst the group of developers I work with, no one knew this trick. I read about it yesterday here. The feature is called the Clipboard Ring, and it is in both Visual Studio 2005 and 2003. Using Ctrl+Shift+V lets you cycle through the last 10 items you have placed on the clipboard. The command is also found in the Edit Menu in and in the Toolbox in VS2003.
Posted: Friday, February 10, 2006 4:52:20 PM (Eastern Standard Time, UTC-05:00)  #    Comments - Trackback
Visual Studio

ADO.Net 2.0 Presentation on January 20th

I will be giving a presentation at a Microsoft Developer Care event at their offices in Southfield, MI on January 20th. The event is sponsored by New Horizons and I will be talking about What's New in ADO.Net 2.0. There will be two other talks as well, see the agenda for details.
Posted: Sunday, January 29, 2006 6:26:58 PM (Eastern Standard Time, UTC-05:00)  #    Comments - Trackback
.Net 2.0 | Speaking

VSTO Excel ListObject Bug

I found an actual bug with the VSTO ListObject in Excel and insertions of data. See this post on the MSDN forums for details.
Posted: Wednesday, January 25, 2006 6:09:38 PM (Eastern Standard Time, UTC-05:00)  #    Comments - Trackback
Office | VSTO

Generics and Enums or Enum.TryParse

I have a case where the user is entering strings into the application, and I have to match that string with the values from a given Enum. Normally that is not too difficult of a task, just convert the enum item to a string and compare, but in this instance I have a bunch of enums to do this with, so I didn't want to write a separate function for each enum type. So I figured generics might provide a solution and here is what I came up with, which is loosely based on the idea of int.TryParse:

/// <SUMMARY>
/// Takes a string that represents an enum member 
///   and returns the enum member
/// </SUMMARY>
/// <TYPEPARAM name="T">An Enum</TYPEPARAM>
/// <PARAM name="input">
///  The string that is the enum member name, case does 
///    not matter
/// </PARAM>
/// <PARAM name="returnValue">
/// The value from the enum that matches the string, or the 
/// first value of the enum /// </PARAM>
/// <RETURNS>
/// True when there is a match, false when not
/// </RETURNS>
/// <REMARKS>
/// - When no match the first item in the enum is returned
/// - The where clause attempts to constrain the input of T 
/// at compile time to be an Enum
/// </REMARKS> private bool GetEnumValue<T>(string input, out T returnValue)
where T : struct, IComparable, IFormattable, IConvertible { if(Enum.IsDefined(typeof(T), input)) { returnValue = (T)Enum.Parse(typeof(T), input, true); return true; } else //input not found in the Enum, fill the out parameter
// with the first item from the enum
{ string[] values = Enum.GetNames(typeof(T)); returnValue = (T)Enum.Parse(typeof(T), values[0], true); return false; } }
Posted: Wednesday, December 07, 2005 7:58:05 PM (Eastern Standard Time, UTC-05:00)  #    Comments - Trackback
.Net 2.0

Awesome IIS Resource

IISToolshed - thanks to Paschal for pointing it out.
Posted: Thursday, December 01, 2005 7:23:36 PM (Eastern Standard Time, UTC-05:00)  #    Comments - Trackback
IIS

Comboboxes and Double-Clicks

The standard combobox control doesn't support the double-click event, as it is actually a combination of more than one control itself. In my case, I am using the combobox in simple mode with a textbox at the top and a permanently open list directly below the textbox. I needed for the listbox below to respond to the user double-clicking a list item. I found a great solution in a Usenet post by Jacques Bourgeois in microsoft.public.dotnet.framework.windowsforms.controls. Essentially the code simulates a double-click using the single-click event and a timer control.

Declare a timer control in your class, and in the load event of the form instantiate the timer set the interval of the timer to the interval of the double-click as set in the user's system:

this.comboDoubleClick = newTimer();

this.comboDoubleClick.Interval = SystemInformation.DoubleClickTime;

In the event handler for the timer, set the code to stop the timer:

this.comboDoubleClick.Enabled = false;

Add code to the single-click event handler to handle the double-click. When the event handler fires, if the timer is not enabled, start the timer (the first click). If the timer is enabled, a previous click has occurred within the system set interval for a double-click. This must be the second click of the double-click, so do whatever you needed the double-click to do and then disable the timer for the next single-click event.

private void Combo_MouseClick(object sender, MouseEventArgs e)
{
   
if (this.comboDoubleClick.Enabled == true)
    {
       
//Do double-click work here
       
this.comboDoubleClick.Enabled = false;
    }
   
else
   
{
       
this.comboDoubleClick.Start();
    }
}

Posted: Thursday, November 10, 2005 6:01:28 AM (Eastern Standard Time, UTC-05:00)  #    Comments - Trackback
.Net 2.0 | Winforms