Why does this code work on Windows 7, but not on Windows XP?

A bit of background: I am a WPF converter for WinForms, and for a while I am porting my application.

A friend told me that my code does not work on Windows XP (it generates a stack overflow on startup), although it works fine on Windows 7 (which I am developing).

After a little research on what caused the problem, there was something like that:

private void listView1_SelectedIndexChanged(object sender, EventArgs e) { listView1.SelectedIndices.Clear(); listView1.Items[0].Selected = true; } 

Now that I noticed a clearly bad solution, I did not wonder why it does not work in Windows XP. I was wondering why this works on Windows 7.

Obviously, at some point, the compiler finds out what I'm trying to do and prevents the same event from firing again, but I would prefer it to not do anything, so I see a squish error on the platform view that I I'm developing, instead of testing it on two platforms at the same time. In WPF, I could handle this behavior manually by setting e.Handled to “true”, apparently there is no such thing in WinForms.

Is there any compiler flag for this?

+7
source share
3 answers

Try the following:

 private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if (!listView1.Items[0].Selected) { listView1.SelectedIndices.Clear(); listView1.Items[0].Selected = true; } } 

You only want to select SET ONCE, in the first item. The problem is that it probably falls into an infinite loop.

As for why Windows 7 is more forgiving than XP, I could not say. There may be an order of processing LVM_ * messages or something else.

+4
source

Check and see if the .NET version has changed. If you have a newer version of .NET on your computer running Windows 7 rather than XP (very likely), then there may be differences even if you target an earlier version.

See what MSDN says about .NET backward compatibility .

+2
source

it can work (NOT TESTED)

 private void listView1_SelectedIndexChanged(object sender, EventArgs e) { if(Environment.OSVersion.Version.Major < 6) listview1.SelectedIndexChanged -= new EventHandler(listView1_SelectedIndexChanged); listView1.SelectedIndices.Clear(); listView1.Items[0].Selected = true; if(Environment.OSVersion.Version.Major < 6) listview1.SelectedIndexChanged += new EventHandler(listView1_SelectedIndexChanged); } 

edit See your OS specifics: o

+1
source

All Articles