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.DisplayName problem

ListBox.DisplayName problem

Scheduled Pinned Locked Moved C#
designhelpquestion
3 Posts 2 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.
  • R Offline
    R Offline
    reza assar
    wrote on last edited by
    #1

    hi all i have a ListBox and i want it to receive a Pair object and show me the Second value of the Pair in ListBox but it shows me the ToString() method of Pair "System.Web.UI.Pair" witch means the property name is incorrect what should i do? my code is : ArrayList list = new ArrayList(); list.Add(new Pair("A", "B")); lstBox.DataSource = list; lstBox.DisplayMember = "Second";

    H 1 Reply Last reply
    0
    • R reza assar

      hi all i have a ListBox and i want it to receive a Pair object and show me the Second value of the Pair in ListBox but it shows me the ToString() method of Pair "System.Web.UI.Pair" witch means the property name is incorrect what should i do? my code is : ArrayList list = new ArrayList(); list.Add(new Pair("A", "B")); lstBox.DataSource = list; lstBox.DisplayMember = "Second";

      H Offline
      H Offline
      Heinzzy
      wrote on last edited by
      #2

      Unfortunately Pair contains only public fields named First and Seconds, but DisplayMember specify a name of the PROPERTY to display. So you could make it work like this

      {
      //form class

      private void Form1_Load(object sender, EventArgs e)
      {
      ArrayList list = new ArrayList();
      MyPair f = new MyPair("A", "B");
      list.Add(f);
      listBox1.DataSource = list;
      listBox1.DisplayMember = "second";
      }
      }

      class MyPair
      {
          public object First { get; set; }
          public object Second { get; set; }
          public MyPair(object a, object b)
          {
              this.First = a;
              this.Second = b;
          }
      }
      
      R 1 Reply Last reply
      0
      • H Heinzzy

        Unfortunately Pair contains only public fields named First and Seconds, but DisplayMember specify a name of the PROPERTY to display. So you could make it work like this

        {
        //form class

        private void Form1_Load(object sender, EventArgs e)
        {
        ArrayList list = new ArrayList();
        MyPair f = new MyPair("A", "B");
        list.Add(f);
        listBox1.DataSource = list;
        listBox1.DisplayMember = "second";
        }
        }

        class MyPair
        {
            public object First { get; set; }
            public object Second { get; set; }
            public MyPair(object a, object b)
            {
                this.First = a;
                this.Second = b;
            }
        }
        
        R Offline
        R Offline
        reza assar
        wrote on last edited by
        #3

        thank you for your attention i really love this site with its responsible people

        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