Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7731723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T06:33:11+00:00 2026-06-01T06:33:11+00:00

I have a databound listbox on my C# WinForm that holds strings that are

  • 0

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.

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?

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).

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 & 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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T06:33:12+00:00Added an answer on June 1, 2026 at 6:33 am

    I am not sure and this would have to be tested, but this article makes me believe that what I am going to say is true.

    Basically, if you go by the MSDN documentation:

    Occurs when a visual aspect of an owner-drawn ListBox changes.

    So, this means that everytime an item is added, this event is called. Also, I think even when you perform certain drawing actions within this method, it will call itself (you might be able to avoid this using SuspendLayout and ResumeLayout on the listbox while updating), but am not sure.

    Here is the kicker as far as I know it. Everytime this event is triggered, it is pretty much for every item in the list. (This might be useful in that you can de-color a previously selected item, so dont jump directly to what I am about to suggest without thinking it through). So, the DrawItemEventArgs has an index of the item being drawn. Using that, you can focus in on a specific item that you need to draw. That might help you from re-processing something that has already been processed (keep in mind the notes from the above article..copied below, about index being allowed to be -1).

    To visualize this process:

    Add 1 item  ->DrawItem
    
    Add 2nd item->DrawItem
                ->DrawItem
    
    Add 3rd item->DrawItem
                ->DrawItem
                ->DrawItem
    
    Add nth item->DrawItem * n
    

    So, this actually results in a kind of fibonacci type situation (3 items resulted in 6 calls…5 would result in your 15 number), and you can see how the initial load could be cumbersome, and how adding a new item make for n calls to the method.

    From the article above:

    The ListBox calls the DrawItem method repeatedly, for each item in its
    Items collection.

    The DrawItemEventArgs argument to the DrawItem event handler exposes
    an Index property whose value is the index of the item to be drawn.
    Watch out! The system raises the DrawItem event with an index value of
    -1 when the Items collection is empty. When that happens you should call the DrawItemEventArgs.DrawBackground() and DrawFocusRectangle()
    methods and then exit. The purpose of raising the event is to let the
    control draw a focus rectangle so that users can tell it has the
    focus, even when there are no items present. The code traps for that
    condition, calls the two methods, and then exits the handler
    immediately

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a listbox that is databound to a Collection of objects. The listbox
I have a ListBox in a grid that gets databound when the page loads...
I have a databound ListBox that is behaving strangely. The ListBox's SelectionMode property is
I have a listbox which is databound to a collection of objects. I want
I have a data bound Listbox as shown below. I want the textblock that
I have a databound dgv that contains combobox columns. When the user selects an
I have a databound ListBox with a DataTemplate setup. The DataTemplate contains a Grid
I have a databound listbox which is actually displaying two columns of data. It
I have a ListBox which uses a DataTemplate to render databound items. The XAML
How do i databind a listbox to a List that i have in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.