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. Reading from a ListBox

Reading from a ListBox

Scheduled Pinned Locked Moved C#
4 Posts 3 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.
  • W Offline
    W Offline
    Wamuti
    wrote on last edited by
    #1

    Hi. I have a listbox named lbDat. I want to read values from the listbox by using the line

    lbDat.SelectedItem.ToString();

    But it just gives me the namespace of my solution and a class name. What am i doing wrong and what can i do to correct this behaviour. Thank you in advance.

    Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

    L H 3 Replies Last reply
    0
    • W Wamuti

      Hi. I have a listbox named lbDat. I want to read values from the listbox by using the line

      lbDat.SelectedItem.ToString();

      But it just gives me the namespace of my solution and a class name. What am i doing wrong and what can i do to correct this behaviour. Thank you in advance.

      Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      ToString on an object will get you the object type unless you override the ToString method. If the item with in the listbox is already a string then there is no need for the ToString. If the item is not a string you should cast it to what object type it is then access the properties you need.

      var castedObject = (CustomObject)lbDat.SelectedItem;
      var dataInObject = castedObject.SomeProperty;

      Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

      1 Reply Last reply
      0
      • W Wamuti

        Hi. I have a listbox named lbDat. I want to read values from the listbox by using the line

        lbDat.SelectedItem.ToString();

        But it just gives me the namespace of my solution and a class name. What am i doing wrong and what can i do to correct this behaviour. Thank you in advance.

        Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

        H Offline
        H Offline
        Herboren
        wrote on last edited by
        #3

        lv.SelectedItems[0].ToString() Dont forget to specify the position of the columns using [0] even if you only have one columns the [0] is still used. Then so on if you have more than one columns [1],[2] etc

        modified on Tuesday, August 23, 2011 12:35 PM

        1 Reply Last reply
        0
        • W Wamuti

          Hi. I have a listbox named lbDat. I want to read values from the listbox by using the line

          lbDat.SelectedItem.ToString();

          But it just gives me the namespace of my solution and a class name. What am i doing wrong and what can i do to correct this behaviour. Thank you in advance.

          Wamuti: Any man can be an island, but islands to need water around them! Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          As another alternative; override the .ToString function of the class that you're stuffing in there (identified by the namespace and classname showing currently) and have it return a formatted string with the most interesting properties of the "this class", somewhat similar to the example below;

          public override string ToString()
          {
          return string.Format("Class myClassName, id: {0}, name: {1}", this.someId, this.someName);
          }

          Overriding that method has the bonus-advantage you have a string-representation in the debugger for your object.

          Bastard Programmer from Hell :suss:

          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