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. ListBox and objects

ListBox and objects

Scheduled Pinned Locked Moved C#
help
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.
  • S Offline
    S Offline
    student_rhr
    wrote on last edited by
    #1

    I have an ArrayList populated with UserControl objects. I need to display this list on a windows form and when a user clicks on any of the items I need display the properties of the control. I thought I could use ListBox control since we can populate it with the objects. But the problem is that when I populate the list box I dont get any text values. And if you click on the list box you can clearly see where the items were populated but no textual representation for it.

    foreach(UserControl c in mb.UserControls)
    {
         listBox1.Items.Add(c);
    }
    

    unless i do:

    foreach(UserControl c in mb.UserControls)
    {
         listBox1.Items.Add(c.ToString());
    }
    

    Its something really simple but for some reason I am not getting it. Would appreciate some help. Thanks.

    L N L 3 Replies Last reply
    0
    • S student_rhr

      I have an ArrayList populated with UserControl objects. I need to display this list on a windows form and when a user clicks on any of the items I need display the properties of the control. I thought I could use ListBox control since we can populate it with the objects. But the problem is that when I populate the list box I dont get any text values. And if you click on the list box you can clearly see where the items were populated but no textual representation for it.

      foreach(UserControl c in mb.UserControls)
      {
           listBox1.Items.Add(c);
      }
      

      unless i do:

      foreach(UserControl c in mb.UserControls)
      {
           listBox1.Items.Add(c.ToString());
      }
      

      Its something really simple but for some reason I am not getting it. Would appreciate some help. Thanks.

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      Any time you want a list of thing more complex than strings you are likely better off to use the ListView Control

      led mike

      1 Reply Last reply
      0
      • S student_rhr

        I have an ArrayList populated with UserControl objects. I need to display this list on a windows form and when a user clicks on any of the items I need display the properties of the control. I thought I could use ListBox control since we can populate it with the objects. But the problem is that when I populate the list box I dont get any text values. And if you click on the list box you can clearly see where the items were populated but no textual representation for it.

        foreach(UserControl c in mb.UserControls)
        {
             listBox1.Items.Add(c);
        }
        

        unless i do:

        foreach(UserControl c in mb.UserControls)
        {
             listBox1.Items.Add(c.ToString());
        }
        

        Its something really simple but for some reason I am not getting it. Would appreciate some help. Thanks.

        N Offline
        N Offline
        Not Active
        wrote on last edited by
        #3

        Listbox.Items is an ObjectCollect so it can store strings as well as ListBoxItems


        only two letters away from being an asset

        1 Reply Last reply
        0
        • S student_rhr

          I have an ArrayList populated with UserControl objects. I need to display this list on a windows form and when a user clicks on any of the items I need display the properties of the control. I thought I could use ListBox control since we can populate it with the objects. But the problem is that when I populate the list box I dont get any text values. And if you click on the list box you can clearly see where the items were populated but no textual representation for it.

          foreach(UserControl c in mb.UserControls)
          {
               listBox1.Items.Add(c);
          }
          

          unless i do:

          foreach(UserControl c in mb.UserControls)
          {
               listBox1.Items.Add(c.ToString());
          }
          

          Its something really simple but for some reason I am not getting it. Would appreciate some help. Thanks.

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

          Hi, a ListBox is great at showing a collection of objects in a vertical list. There are basically two ways to visualize the items: 1. provide a ToString() method that returns the string you want; it could show whatever member(s) of the item, or some other calculated value. This way always renders a single string, all in one color. 2. or make the entire ListBox userdrawn; by providing a paint handler to the DrawItem event, you can paint anything you like, even the actual look of the UserControl itself... You probably will want to show selected items in a different way than unselected ones, e.g. a different background color. :)

          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.


          S 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, a ListBox is great at showing a collection of objects in a vertical list. There are basically two ways to visualize the items: 1. provide a ToString() method that returns the string you want; it could show whatever member(s) of the item, or some other calculated value. This way always renders a single string, all in one color. 2. or make the entire ListBox userdrawn; by providing a paint handler to the DrawItem event, you can paint anything you like, even the actual look of the UserControl itself... You probably will want to show selected items in a different way than unselected ones, e.g. a different background color. :)

            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.


            S Offline
            S Offline
            student_rhr
            wrote on last edited by
            #5

            well I ended up doing something like this:

                        Dictionary<int, UserControl> dic = new Dictionary<int, UserControl>();
            ...
                        int count = 0;
                        foreach (UserControl c in mb.UserControls)
                        {
                            listBox1.Items.Add(c.Name);
                            dic.Add(count, c);
                            count++;
                        }
            

            and when user double clicks a control I simply use the selectedIndex to pull the control out from dictionary

                        foreach (KeyValuePair<int, UserControl> kvp in dic)
                        {
                            if (kvp.Key == listBox1.SelectedIndex)
                            {
                                MessageBox.Show(kvp.Value.Name, kvp.Value.Size.ToString());
                                winForm.Controls.Add(kvp.Value);
                            }
                        }
            

            is that a terrible solution?

            L 1 Reply Last reply
            0
            • S student_rhr

              well I ended up doing something like this:

                          Dictionary<int, UserControl> dic = new Dictionary<int, UserControl>();
              ...
                          int count = 0;
                          foreach (UserControl c in mb.UserControls)
                          {
                              listBox1.Items.Add(c.Name);
                              dic.Add(count, c);
                              count++;
                          }
              

              and when user double clicks a control I simply use the selectedIndex to pull the control out from dictionary

                          foreach (KeyValuePair<int, UserControl> kvp in dic)
                          {
                              if (kvp.Key == listBox1.SelectedIndex)
                              {
                                  MessageBox.Show(kvp.Value.Name, kvp.Value.Size.ToString());
                                  winForm.Controls.Add(kvp.Value);
                              }
                          }
              

              is that a terrible solution?

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

              Hi, if it suits your needs, then it is fine. One remark though: since all your keys are different (as they should), and a dictionary has an indexer (i.e. can be read as if it were an array), there is no need to search for a specific item, you could simply do:

              int index=listBox1.SelectedIndex;
              if (index>=0) {
              UserControl uc=dic[index];
              MessageBox.Show(uc.Name, uc.Size.ToString());
              winForm.Controls.Add(uc);
              }

              BTW: you don't really need a Dictionary, a simple List< UserControl> would work the same way. :)

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