Weird listview phenomena

R

Rune Jacobsen

Hi all,

I think I have just witnessed the strangest thing ever. Sort of.

I have a complex solution upgraded from VS 2003 / .Net 1.1 to VS 2005
(Sp1) / .Net 2.0. The solution consists of a few projects; Some GUI apps
and a few class libraries.

Most stuff works fine, however, I see a very strange problem in the main
GUI app.

In it I have a listview in details/report mode, with 4 columns and a
number of items. These items have UseItemStyleForSubItems all set to
false, because the different items and different columns can all be
colored differently and appear in bold and/or italics depending on their
content.

In VS 2003 this worked perfectly.

In VS 2005, the colors/styles seem to only take partial effect - some
are painted in the proper color/style, others are black and in the
normal font. However, if I move the window off screen or move another
window in front of it and then away, it all looks the way it is supposed
to! If I then click a row, the row I click (plus the one that just got
deselected) returns to being black/normal font.

Ok, so this is weird enough by itself - I can't see why the listview
should behave like this, and of course I checked I am not using some
weird subclassed listview, we're talking System.Windows.Forms.ListView
all the way.

So I made a new solution, with a new project. Added a listview, added
three columns, set it to report/details mode (and yes, that is ALL I did
in the designer). Then I pasted the following simple test code after the
call to InitializeComponent():

for (int i = 1; i <= 10; i++)
{
string m = i.ToString();
ListViewItem itm = new ListViewItem("Item " + m);
itm.SubItems.Add("Fun " + m);
itm.SubItems.Add(m + " stuff");
itm.UseItemStyleForSubItems = false;
itm.SubItems[0].ForeColor = Color.Green;
itm.SubItems[1].ForeColor = Color.Red;
itm.SubItems[2].ForeColor = Color.Blue;
listView1.Items.Add(itm);
}

And what do you know - it works!

So... something is strange in my converted solution, I guess...?

I did exactly the same in one of the existing windows of one of my test
application. The problem occured.

I created a new Windows Form, added a listview and performed the same
steps as above. The problem occured.

I then added a new project to my solution, added a form to it, then
added a listview and the same steps as above. Now it didn't happen -
this time everything worked fine!

So the only conclusion I can draw is that *something* bad is in the
project definition files from my old project that keeps the subitems
from keeping their style/color.

Does anyone know how I can fix this? I would love to be able to have
colored/styled subitems in my listview without having to create
completely new projects...

Thanks in advance!

Rune
 
J

Jared

Creating new project should't be that much of drama, just drop and drag all
your code files into new empty project.
 
R

Rune Jacobsen

Jared said:
Creating new project should't be that much of drama, just drop and drag
all your code files into new empty project.

Hi there,

Thanks for your suggestion - it helped me figuring it out! It was
actually not that hard, but like many things, if you don't already know
it, it is difficult to find out. Google wasn't much help either.

As per your suggestion I started a new project (and it was going to be a
looong day..), and I noticed in the new project that Visual Studio had
created a Program.cs file that was obviously the startup point of the
application. In this file I found two lines right before the initial
form was created:

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

I put this in my own app, right before I create my first window, and all
now works.

I still think this is an obscure situation, but I guess it's all for the
best. ;)

Thanks for pushing me in the right direction!

Rune
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top