Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Listbox.Drawitem get called many times?
    primarykey
    data
    text
    <p>I have a databound listbox on my C# WinForm that holds strings that are links to image file locations. I want to display the image as a thumbnail for the user to click on and view. I got it working correctly by setting DrawMode=OwnerDrawVariable and handling the DrawItem and MeasureItem events. </p> <p>However I noticed that I have to click exit 2 times to get out of the application (looks like it's called selectedIndexChanged on first click, then exits on second). On further inspection I noticed that the DrawItem event is fired a multitude of times when I click an item in the listbox (like 15+ times). There is only 1-2 items EVER in the listbox at a time! Why is it getting called soo many times? </p> <p>I tested this with a non databound simple listbox and it does the same thing. I am only curious as I have to read the image from disk and get a thumbnail of it to put into the listbox, which if it did that 15-20 times could affect performance (and is TOTALLY unnecessary).</p> <pre><code>private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e) { MessageBox.Show("listBox1_MeasureItem"); // Cast the sender object back to ListBox type. ListBox theListBox = (ListBox)sender; // Get the file path contained in each item. DataRowView drv = (DataRowView)theListBox.Items[e.Index]; string fileString = drv.Row["fullpath"].ToString(); // Create an image object and load image file into it Image img = Image.FromFile(fileString); e.ItemHeight = Convert.ToInt32(img.Height * 0.15); e.ItemWidth = Convert.ToInt32(img.Width * 0.15); } private void listBox1_DrawItem(object sender, DrawItemEventArgs e) { MessageBox.Show("listBox1_DrawItem"); // If the item is the selected item, then draw the rectangle // filled in blue. The item is selected when a bitwise And // of the State property and the DrawItemState.Selected // property is true. if ((e.State &amp; DrawItemState.Selected) == DrawItemState.Selected) { e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds); } else { // Otherwise, draw the rectangle filled in beige. e.Graphics.FillRectangle(Brushes.Beige, e.Bounds); } DataRowView drv = (DataRowView)listBox1.Items[e.Index]; Image img = Image.FromFile(drv.Row["fullpath"].ToString()); img = img.GetThumbnailImage(e.Bounds.Width, e.Bounds.Height, null, IntPtr.Zero); e.Graphics.DrawImage(img, e.Bounds.X, e.Bounds.Y); // Draw the focus rectangle around the selected item. e.DrawFocusRectangle(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload