listbox object display

hi...

using vb.net 2003...

newbie here...  i tried searching the forums and could not find my answer...  sorry for the inexperience i am about to bring... 

anyway, i am trying to build a listbox of objects and have a string displayed, but also store a tag as well to save the key info...  i initially used a treenode object, but it displays "treenode: ....."   i don't want "treenode" displayed...

then i tried creating my own object, but it wouldn't display the text...  it would only say form1.listbox1.object....  in the listbox...  how would i get a string to display while storing the key info with the object

thanks for any help...


Answer this question

listbox object display

  • paxa

    thanks!!  that worked great!!
  • Mohamad Shehadeh

    We'd have to see the text to know for sure. It think there are other ways around this.

    if you write:  Dim obj as object = "Stop War"

    You may have to   Dim foo as string = ctype(foo, string)

    But.... I think there's a better way.

    At least listview controls have a tag field for each item.

    I use the tag field which is an object that you can put anything in, as a data descriptor field.

  • UntiedLogic

    I was hoping that the listbox items had tags but it doesn't. That's why I don't like list boxes.


    OK Plan B.

    When you get the string with "treenode in them... is that data you want also there

    If so, you can do this   stringobject.replace("StringYouWantToEliminate","")

  • fueidosffjewoei

    i'm trying to stay away from the listview control as i'm going to try to convert this application to asp.net when i'm done and i don't think the listview control is included in the toolbox for asp.net...

    yeah, i know, i could download it or create my own, but i'm not that advanced yet...

    so is there no way to display an object's text property in a listbox

    example:

    Public Structure ListNode
       Public Text As String
       Public Tag As String
    End Structure
    '-----------------------------------------------------------------
    ' LOAD LISTBOX
    '-----------------------------------------------------------------
    Dim L As New ListNode
    For Each dr In ds.Tables("CUSTOMER").Rows
       L.Text = dr.Item("CUSTOMER_NAME")
       L.Tag = dr.Item("KEY")
       listbox1.Items.Add(L)
    Next

       
    when i execute this, the listbox just displays something like form1.listbox1.listnode...

    how can i get it to just show the text property...

    thanks again for any help...

  • jrobuild

    i tried loading the listbox with treenodes and it achieves my result, but....

    it displays as follows:
       Treenode: CUSTOMER A
       Treenode: CUSTOMER B

    that's pretty annoying...

    is there any way to get the literal "Treenode" to not show up

    can't do a replace as "Treenode" isn't in any string...

    this seems like such a simple task...  am i missing something here


  • SaravananVV

    You can set the "DisplayMember" property of the listbox to a field of your liking or override the ToString property of your treeview (more complicated).  I created my own ListItem object that I use in all combo/listboxes:

    Public Structure ListItem
         Public Text As String
         Public Value As Integer
         Public Tag As String
         Public Sub New(ByVal sText As String)
              Text = sText
         End Sub
         Public Sub New(ByVal sText As String, ByVal iValue As Integer, Optional ByVal sTag As String = "")
              Text = sText
              Value = iValue
              Tag = sTag
         End Sub
         Public Overrides Function ToString() As String
              ToString = Text
         End Function
    End Structure

    This way, you can load pretty much any information into the object.

    Dim poItem as ListItem
    Dim piX as integer

    For piX = 0 to 10
       poItem = New ListItem("Entry " & piX, piX)
       Listbox1.Items.Add(poItem)
    Next

    To reference it, it's simply Listbox1.Items(1).Value or Listbox1.Items(3).Tag, etc.

     


  • listbox object display