Resolved Division by zero when inserting items in a ListView

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello everybody,

I got my machine back after the hard disk has been changed.
I improved a few programs, everything OK.
I developed a program to list the files in a directory, the user wants the second line of the file to be listed, it was no problem to display that in a RichTextBox (except I should like to have a column in a different font, but perhaps I did not choose the good control for that).

Here is now where things become serious, whereas I think it should be simple.

I want to add the file names in a ListView, and I obtain a division by zero error at the third one, and I do not understand why.

So, I created a new project named TestListView, WinForms based on .Net 4.7.2, on the form I added a ListView and a button, with this code for the button :
Inserting items in a ListView with a button:
        private void btnAddItems_Click(object sender, EventArgs e)
        {
            for(int i = 0; i < 10; i++)
            {
                try
                {
                    listView1.Items.Add(i.ToString());
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

The two first items are added OK, at least I presume as they do not appear on the screen, but for the third element, the try...catch is ignored and I get a divide by zero error on line 7.

So I tried with the visual interface, and I also got an error when attempting to create the third element. Then I could insist, and create other elements after ignoring the error, but the application has the exception when starting.

After that, in the development interface the ListView has this aspect :
ListView with error Capture d’écran 2022-09-11 163525.png


I saw nothing interesting in the eventViewer, uninstalled and reinstalled .Net desktop workload from Visual Studio 2022 (the "Framework" one, and the other one), nothing changed.

Anything I missed ?
 
Last edited:

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Oh I think I got it.
By default the ListView is created to display in LargeIcon mode, and I did not pay attention to this point, so I did not provide any picture. As an error message I should have prefered "a picture is needed in this display mode" rather than "division by zero error", but ... once I found what to do, it works.
Once the View property is set to Details, there is no more error message, and once I provide a column, the contents is displayed.

Thank you to have read.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
The WinForms ListView is just a thin wrapper around the Win32 Commont Control ListView with the objective of keeping it thin instead of adding extra logic. It looks like the underlying control may have been the cause of the issue.
 

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Hello,
I wonder whether the WinForms ListView could verify the display mode to begin with, and in case it is LargeIcon or SmallIcon, verify it has correct icons.
If it does not, you have the richness of information a beginner user would provide you : "It does not work."
 
Last edited:

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
It could, but that goes against the objective of being a thin wrapper.

Also it looks like WINE, a Windows Emulator for other OSes, had the same division by zero bug in their comctl32 code. Someone submitted a fix for it back in 2004. Based on your report, it looks like the real Windows common control still has the bug.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
But since .NET (Core) is now open source, and WinForms was added in .NET Core 3.0, you can submit a patch with your proposed fix to validate the display mode.
 

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
And then, I presume it is a better idea to create a project based on .Net Core.

Anyway, Windows 10 needs many urgent patches (that I suppose Windows 11 did not even consider, and I heard, it even added other problems), so that leaves less time to debug the API controls.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
6,068
Location
Chesapeake, VA
Programming Experience
10+
My opinion is that .NET Core is at par with Docker. Not quite ready for primetime and for use by the masses for production use. It is usable and can be used if you are willing to live with or workaround various idiosyncrasies (bugs, missing functionality, lack of documentation, undocumented or changing behavior, etc.). Don't get me wrong. It is possible to be successful with it, but it requires a higher level of engagement (keeping up changes, willingness to look at source code of your depencies, deeper understanding of how all your depencies work, etc.).
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,503
Location
Norway
Programming Experience
10+
So, I created a new project named TestListView, WinForms based on .Net 4.7.2, on the form I added a ListView and a button, with this code for the button :
Tested this and could not reproduce the problem.
 

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Yes you can if you carefully read the thread.
This depends on the View property of the ListView, which is an enumeration with five possible values, in the order displayed in the drop-down list in the properties window, they are LargeIcon, Details, SmallIcon, List, Tile.

If you have Details in that property, there is no problem and the ListView is created and your application compiles and executes properly.

If as a view you select LargeIcon or SmallIcon (or you got one of them by default and did not pay attention), this means that the items of the ListView will be displayed under the form of an icon with a text reproducing the first parameter you provide when creating the item. So, in those two views, if you forget to provide icons to display the items, you will have a problem to display them.

I got the exception for the third item. I did not deepen the reasoning, but a possible explanation is that two default icons are provided, and that by a mysterious way (that will be less mysterious after reasoning more deeply), the selected icon is depending on the item index, so once you created more items than you have icons, bam!

I did not test the two other views, List and Tile, but I presume they can function with no icons, and for that reason I should not be surprised there is no exception when running the project with them.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
1,503
Location
Norway
Programming Experience
10+
By default the ListView is created to display in LargeIcon mode,
Yes, I did read that. No problem with that for me. listView1.Items.Add(i.ToString()) only sets the Text of the item, the ImageIndex remains default -1.
 

Gloops

Well-known member
Joined
Jun 30, 2022
Messages
137
Programming Experience
10+
Oh, so possibly there is another property that plays a role in there (or possibly the existence of an ImageList, and it not obvious why one project has been created with one by default, and another without). This time, the deep reasoning will be useful to answer correctly.
 
Top Bottom