WinForms ComboBox does not always fire SelectionChangeCommitted

Today I ran into the problem that a ComboBox in my project did not always fire the SelectionChangeCommitted event when I expected it to. To be specific, if a user dropped down the list, used the arrow keys to move to an item, and then TABbed to the next control, the new item would be selected but the event never fired. This was a problem for me, because I relied on this event to filter the items in the next control, depending on the selection made in the first.

Some googling quickly learned that this was a known problem with the ComboBox, although the thread is about the SelectedIndexChanged event, it appears the same error prevents SelectionChangeCommitted from behaving as expected. As user comecme in that thread pointed out, there even was a knowledgebase item including a workaround available from Microsoft. But as he also points out… the workaround does not work.

So I decided to cook up my own solution. This would involve subclassing the Combobox to change the behavior I needed. I had one advantage, I was already using a subclassed ComboBox in all places I needed this behavior, so no Find+Replace was required to replace all standard ComboBoxes with my own.

To this subclassed combobox (named CatalogComboBox) I added the following code:

private CatalogComboItem keepSelection;

protected override void OnEnter(EventArgs e) {
  keepSelection = this.SelectedItem;
  base.OnEnter(e);
}

protected override void OnLeave(EventArgs e) {
  if (this.SelectedItem != keepSelection)
    OnSelectionChangeCommitted(EventArgs.Empty);
  base.OnLeave(e);
}

protected override void OnSelectionChangeCommitted(EventArgs e) {
  base.OnSelectionChangeCommitted(e);
  keepSelection = this.SelectedItem;
}

(I used CatalogComboItem because my combo items where typed as such, but you can always use object.) It’s a not spectacularly elegant or high-tech solution, but it works like a charm.

Advertenties

Firefox 4 released

The Mozilla team released the final version 4 of Firefox (after 10+ betas or so).

I like the fact it checks in advance if your plugins will be compatible, which would be the major reason for me to not upgrade if they weren’t. Looks like I’m alright for all plugins that really matter to me, the remaining ones are a Glass plugin and Compact menus which are irrelevant for Firefox 4; I haven’t used the VMWare remote console plugin for VMWare Server since switching to Workstation several months ago; and Firebug 1.6.2 can easily be upgraded to 1.7.

I am going to hit that Update button soon.

The case of the Visual Studio blank properties window

I experienced the strangest thing with my installation of Visual Studio 2008 last Friday. In the middle of developing a Windows Forms application, I noticed that the Properties window had stopped working. It was still there, just blank, and so was the dropdown above it where the components usually reside.

Update 19-12: This blog post will be revised soon. I have since experienced this same error under different conditions which had a different solution.

Update 20-12: This blog post is superseeded by this one. I have now found a generic strategy to follow if this situations occurs. The solution presented below was probably a one-time fix that will most probably not help you. Please read the new article.

Blank Properties window in Visual Studio

This was not just with the current file, but for all files in my solution. It would not even display the mainly informal properties of the current file selected in the Solution Explorer. Closing the solution and re-opening it did not help. Hiding the properties window and re-showing it did not help. Google led me to several suggestions, most of them telling me to execute a command in the Visual Studio command prompt (devenv /setup) would fix things. Nope, not for me. Starting VS in safe mode to eliminate plugins. Still blank. Even other solutions I opened now had a blank properties window. Oh dear. I was now getting worried that I broke something very serious in my VS installation.

Lees meer over dit bericht

Hello world!

Welcome to WordPress.com. This is your first post. Edit or delete it and start blogging!

I think I’ll leave this message as is. I consider it a fitting title for a blog about software development. 🙂