views:

1731

answers:

7

Using C# .NET 2.0, I have an owner-drawn ListView where I'm overriding the OnDrawColumnHeader, OnDrawItem and OnDrawSubitem events. If I set the View property to Details at design-time, everything works beautifully and I can switch the View property and all view modes display as they should (I'm not using Tile view). However, if I start in any other View, both the List and Details views are blank.

I know you'll probably want to see code, but there's a lot of it, so I'm hesitant to post that much, but can if necessary. I'm more curious if someone has seen this before, and/or might have an inkling of how to fix it. The View property will be a user-saved setting, so I won't always be able to start in Details view by default.

A: 

NOTE: Just woke up 2 minutes ago, so this is all just what my subconscious thinks

My gut says you may not be adding SubItems to the items.

Alex Lyman
+1  A: 

Either SubItems are not added, or you didn't add any columns. That's my initial feeling.

Jeff Yates
A: 

Sorry, maybe I wasn't clear. When I said everything works if I default it to Details, I do mean everything. All the subitems and columns render exactly as they should. Once the app is running, changing to a different view also works, and everything renders perfectly. It's only when I start the app in any other View that when switching to Details or List the views come up blank. (If I start in List view, only Details comes up blank.)

Image on the left is when I start in Details, image on right when I start in LargeIcon and switch to Details.

Like I said, I can post code, but otherwise it does work, provided I start in Details view.

Thanks for the responses.

Michael Itzoe
Post a minimal example that illustrates your problem, please. Regards, tamberg
tamberg
A: 

If you configure it correctly using the designer, just go into the generated designer code and see what code was emitted by Visual Studio to get it to work right. THen just emulate that code.

dviljoen
A: 

Withour your code, there is nothing much can be said but DrawColumnHeader is only called when OwnerDraw property is set to true. Not sure if it is automatically set to true and false depending on the View property but it is worth giving a try. So make sure OwnerDraw is set to true before launching your application.

Serhat Özgel
+1  A: 

The WinForms ListView is mostly a layer of abstraction of the top of the actual Windows control, so there are aspect of its behaviour that are, well, counterintuitive is a polite way of putting things.

I have a vague recollection, from back in my days as a Delphi developer, that when you are Owner drawing a ListView, the subitems of the control aren't actually populated unless your Listview is in "Details" mode when you load the items.

Things to try ...

... force the WinForms control to recreate the underlying windows handle after you change the display style. If memory serves, DestroyHandle() is the method you want.

... assuming you have a "Refresh" in your application to reload the data, do things work properly when you refresh after changing the display style?

... if all else fails, beg borrow or steal a copy of Charles' Petzolds classic on windows programming.

Bevan
A: 

I posted this question before I registered my account, so I can't mark it closed or vote up or anything (I don't think, please correct me if I'm wrong.)

Anyway, thanks, Bevan. DestroyHandle led me to RecreateHandle, and that fixed the problem. Thanks, too, to everyone that answered.

Michael Itzoe