Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to Refresh a ListBox Contents ?

How to Refresh a ListBox Contents ?

Scheduled Pinned Locked Moved C#
helptutorialquestionannouncement
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MehmetFurkan
    wrote on last edited by
    #1

    Hi, I am using a ListBox on my form. My ListBox.Items property (which is ObjectCollection) contains objects of my-defined class type, whose ToString() methods are overriden. As I know, a ListBox calls the ToString() method of each item in its Items property to build a "string" to display. When adding a new object item ot Items affects display strings as a result. So far everything is good and reasonable. The problem is that, when I MODIFY an object's content held by ListBox.Items, I expect the ListBox control redraw its content. Cause my modification affects the overridden ToString() method of an object of ListBox.Items. So I expect that the ListBox control to change display string of related object. Unfortunately it does not work even if I have tried to call ListBox.Refresh(), or Update() method. Any other method I should call or my way thinking about ListBox.Items and displaying mechanism is wrong?

    J D 2 Replies Last reply
    0
    • M MehmetFurkan

      Hi, I am using a ListBox on my form. My ListBox.Items property (which is ObjectCollection) contains objects of my-defined class type, whose ToString() methods are overriden. As I know, a ListBox calls the ToString() method of each item in its Items property to build a "string" to display. When adding a new object item ot Items affects display strings as a result. So far everything is good and reasonable. The problem is that, when I MODIFY an object's content held by ListBox.Items, I expect the ListBox control redraw its content. Cause my modification affects the overridden ToString() method of an object of ListBox.Items. So I expect that the ListBox control to change display string of related object. Unfortunately it does not work even if I have tried to call ListBox.Refresh(), or Update() method. Any other method I should call or my way thinking about ListBox.Items and displaying mechanism is wrong?

      J Offline
      J Offline
      Justin Perez
      wrote on last edited by
      #2

      Maybe my thinking here is wrong, but how about simply clearing the listbox and re-add your object collection?

      I'm going to become rich when I create a device that allows me to punch people in the face over the internet. "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer

      M 1 Reply Last reply
      0
      • J Justin Perez

        Maybe my thinking here is wrong, but how about simply clearing the listbox and re-add your object collection?

        I'm going to become rich when I create a device that allows me to punch people in the face over the internet. "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer

        M Offline
        M Offline
        MehmetFurkan
        wrote on last edited by
        #3

        Ok, thanks. I have just tried and it works.

        J L 2 Replies Last reply
        0
        • M MehmetFurkan

          Ok, thanks. I have just tried and it works.

          J Offline
          J Offline
          Justin Perez
          wrote on last edited by
          #4

          Great :)

          I'm going to become rich when I create a device that allows me to punch people in the face over the internet. "If an Indian asked a programming question in the forest, would it still be urgent?" - John Simmons / outlaw programmer

          1 Reply Last reply
          0
          • M MehmetFurkan

            Ok, thanks. I have just tried and it works.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            IMO removing and adding the collection is too much effort and causes flickering. Invalidate() should be sufficient, unless you are in an event handler and want an immediate repaint, if so use Refresh(). If that does not work for you, then something is wrong and you may want to show some code. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


            1 Reply Last reply
            0
            • M MehmetFurkan

              Hi, I am using a ListBox on my form. My ListBox.Items property (which is ObjectCollection) contains objects of my-defined class type, whose ToString() methods are overriden. As I know, a ListBox calls the ToString() method of each item in its Items property to build a "string" to display. When adding a new object item ot Items affects display strings as a result. So far everything is good and reasonable. The problem is that, when I MODIFY an object's content held by ListBox.Items, I expect the ListBox control redraw its content. Cause my modification affects the overridden ToString() method of an object of ListBox.Items. So I expect that the ListBox control to change display string of related object. Unfortunately it does not work even if I have tried to call ListBox.Refresh(), or Update() method. Any other method I should call or my way thinking about ListBox.Items and displaying mechanism is wrong?

              D Offline
              D Offline
              darkelv
              wrote on last edited by
              #6

              The displayed text does change for my business object collection when bound to a list box. Are you setting the items manually (using Add) or you set the DataSource to your object collection? Which interface did you implemented on the collection? (IList, BindingList, ICollection etc)? Also check out the ListChangedEventArgs[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups