The case of the blank Visual Studio properties window — part 2

Earlier this year I suffered from a case of a Visual Studio properties window that appeared to get “stuck” in the middle of developing an application. It would show up blank and the only way to get it to work was to fix the project and restart Visual Studio (not just close and reopen the solution). For more details, please read the first article.

Blank Properties window in Visual Studio

Although I haven’t written about it, after applying the solution from the first article, my problems weren’t entirely over. Every once in a while, the properties window would get stuck again, and after tweaking the .xsd file a little or reverting it to a previous revision, I could get it to work again. But I never got the feeling that I really understood what I was doing. It would just work sometimes, or not. I even got hesitant to make any changes to the dataset, unless absolutely necessary. But it only happened on that single solution.

Until last week. In an entirely different solution, all of a sudden the properties window was blank again. It’s subtle, you don’t always notice immediately and because of that I could not simply undo the last the thing I did. This solution contained a dataset file too, so after cursing a little (I admit) the first thing I did was revert that and reapply the changes one by one. But this time, that didn’t work. I reverted the dataset to points way before the property editor got broken but still nothing. It was starting to look like the dataset file being the solution was not a generic one, but just the solution for that first project. This time, it was something else. Lees meer over dit bericht


Error when compiling .resx file – Attempt to load a program with an invalid structure

I have seen this before and fixed it, but ran into it again today and could not remember what is was. When compiling a WinForms application in Visual Studio 2010, I got an error message stating “Kan bestand of assembly (…) of een van de afhankelijkheden hiervan niet laden. Poging om een programma te laden met een onjuiste indeling. ” (Roughly translated, “Can not load assembly (…) or one of its dependencies. Attempt to load a program with an invalid structure.”).

When double-clicking the error, it points to a base64-encoded piece of a resx-file, in this case an image stream for an image list.

This is a bug in Visual Studio 2010 that has not yet been fixed. See here on Microsoft Connect.

The workaround suggested by Pellet21 works for me in the mean time. I’ll copy it here to save myself from Googling for it next time:

1.    Open Form in Designer and make needed GUI changes. Close designer and save
2.    Compile project and receive RESX compile error (only forms with Imagelist should have this problem)
3.    Double-click resx compile error to open resx file.
4.    Scroll to top of imagestream.
5.    Edit the top line of the Image stream:
6.    Close and save resx file and recompile.
**NOTE: the only difference are the characters at end “j00LjAuMC4w’ to “j0yLjAuMC4w”

This needs to be done EVERY TIME you open the form in Designer mode.

Initializing a combobox to a blank value might be harder than you think

Combobox troubles again today. No matter how hard I tried to have Comboboxes in my dialog form to initially display blank (SelectedIndex = -1), as soon as the dialog was displayed, the combos would always show the first item in the List that populated it. Despite being set to -1 explicitly. It was driving me nuts.

When created at runtime using a List as DataSource, getting this (empty combos) might be harder than you think

The situation: I had a dialog form that should have a variable number of comboboxes on it, determined at runtime. So the form had an Init method where I dynamically added the comboboxes, and set its DataSource to a List of items (directly, no BindingSource). The form also has a LoadData method, that sets the SelectedIndex of each combo to the actual value from the data, or to -1 of it was unknown or uninitialized at that time. This was to be able to make the combo appear blank instead of preselecting the first item, which was undesired. The caller of the dialog executes Init(), LoadData() and finally ShowDialog().

I was expecting a blank combobox, but it showed the first item in the list preselected. I triple checked that the SelectedIndex was correctly set to -1 at the end of LoadData(), but as soon as the form was displayed, it was 0. What the *** happened there in between and “who” was secretly setting my property!? Lees meer over dit bericht

Keeping a modeless window accessible from a modal (dialog) window

In a C# WinForms application I was working on, users were effectively working with two more or less independent windows: one they worked in, and another that showed documents and other info they need to do that work. The problem was that when they were in a dialog, for example one to assist in creating a new object, the window holding the documents with info on the object was not accessible because the dialog is modal.

You can have the dialog take ownership of the document window, but that means also it will always be on top of it, which would be inconvenient.

So I borrowed a trick from my Delphi days that I found here. You only need to do a small Windows API call from your code. I added this code to utility class called FormsUtil, but you could of course put it anywhere that’s convenient to you.

private static extern bool EnableWindow(IntPtr hwnd, bool bEnable);

public static bool EnableWindow (Form form, bool enable) {
    return EnableWindow(form.Handle, enable);

This allows you to, in the dialog form, attach an event handler to the Shown event:

private void NewObjectForm_Shown(object sender, EventArgs e) {
    if (this.Modal) {
        var viewer = GetReferenceToDocumentViewerForm();
        FormsUtil.EnableWindow(viewer, true);

Now the document viewer window is accessible from the “new object” dialog.

Tip: be careful if you ever choose to use EnableWindow(formX, false). It will disable that form completely, including not being able to close it.

Respond to a click on the background of a listview

A typical list view, in this case from Explorer. Items are unselected when you click in an empty area.

I have a list view in a project that i use to show properties for objects in a tree-like hierarchy, much like the windows explorer. When a user clicks an item, its properties are shown. That wasn’t so hard, just handle the Click event.

When a user clicks next to an item or in an empty space of the list view, the item is automatically unselected in the view. The list view does this for you. I wanted to use this event, to show the properties of the parent object of the items in the view. But kind of to my surprise no Click event was fired when that happened.

In fact, I couldn’t find another event that really did what I wanted. Some suggested to use the ItemSelectionChanged event, like in this somewhat related problem, but that would require me to track all pairs of selection/unselection and to only respond when an item was unselected, after which no selected event would happen. I figured it would be too tricky (and too much work) to get that right.

So I cooked up another solution. It’s no rocket science, but it works exactly as I would like it to. Here’s how you do that… Lees meer over dit bericht

Jet OleDB 4.0 provider not registered on the local computer

I have a C# WinForms program that attempts to import some data from Excel into a SQL Server database using the Ole DB provider. Today the import suddenly failed with an error message saying that the Jet Ole DB provider was not registered on the local computer.

InvalidOperationException: Jet OleDB 4.0 provider not registered on the local computer

Well, I was pretty sure that it was. So what was wrong then?

Turned out that the Configuration Manager accidentally got reset to “Any CPU” setting. I have an x64 machine on which that leads to running the application in 64-bit mode, in which there is indeed no Jet OleDB provider available (and no Edit-and-Continue either, among other things…).

Setting the target platform to compile to x86 immediately solved the problem.

Open a .cs file in code view instead of winforms designer

This bugged me for a while. It didn’t bug me enough to spend much time looking for it, but every once in a while I would give it a try and then I’d give up again. The problem? When double-clicking a file in the solution explorer, it would open in the windows forms designer. I wanted it to open in code view, but couldn’t find it anywhere in the extensive Visual Studio Options screen. For HTML-view, there was an option, but not for windows forms files. Huh?

That was because I was looking in the wrong place. Right click a .cs file and choose “Open with”. Then select “CSharp editor” and click “Set default”. Duh.

The easy-to-find option, in a really obvious place