Retrieve info from combobox while adding objects of any type ??

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mendhak

    Retrieve info from combobox while adding objects of any type ??

    Hi All,

    When you add an object to a combobox, and run the application, you'll get
    something like this:

    System.Windows. Forms.Button, Text: Button1

    (for example).

    Is it possible to have just the text/value in the object to be displayed in
    the combobox, yet at the same time when retrieving the combobox item,
    getting that same object back?

    In this particular case, an object of ANY type can be added to the combobox.

    TIA.


  • Greg Burns

    #2
    Re: Retrieve info from combobox while adding objects of any type ??

    If I understand you correctly you can you a custom class like so:

    add items like this:
    cbo.Items.Add(N ew ListBoxItem(MyO bject, "display string"))

    retrieve items like this:
    Dim MyObject as Object = CType(cbo.Selec tedItem, ListBoxItem).Da ta

    Public Class ListBoxItem

    Private listItemData As Object
    Private listItemText As String

    ' This is what is displayed in the ComboBox drop-down
    Public Overrides Function ToString() As String
    Return listItemText
    End Function

    Public Sub New(ByVal itemData As Object, ByVal itemText As String)

    listItemData = itemData
    listItemText = itemText
    End Sub

    Public ReadOnly Property Data() As Object
    Get
    Data = listItemData
    End Get

    End Property

    Public ReadOnly Property Text() As String
    Get
    Text = listItemText
    End Get

    End Property

    End Class

    HTH,
    Greg

    "Mendhak" <anonymous@disc ussions.com> wrote in message
    news:eZZtI$hpEH A.1308@TK2MSFTN GP14.phx.gbl...[color=blue]
    > Hi All,
    >
    > When you add an object to a combobox, and run the application, you'll get
    > something like this:
    >
    > System.Windows. Forms.Button, Text: Button1
    >
    > (for example).
    >
    > Is it possible to have just the text/value in the object to be displayed
    > in
    > the combobox, yet at the same time when retrieving the combobox item,
    > getting that same object back?
    >
    > In this particular case, an object of ANY type can be added to the
    > combobox.
    >
    > TIA.
    >
    >[/color]


    Comment

    • Larry Serflaten

      #3
      Re: Retrieve info from combobox while adding objects of any type ??


      "Mendhak" <anonymous@disc ussions.com> wrote[color=blue]
      >
      > When you add an object to a combobox, and run the application, you'll get
      > something like this:
      >
      > System.Windows. Forms.Button, Text: Button1
      >
      > (for example).[/color]


      How are you adding objects to the combobox, before running the application?

      LFS

      Comment

      • Jay B. Harlow [MVP - Outlook]

        #4
        Re: Retrieve info from combobox while adding objects of any type ??

        Mendhak,
        As Greg suggests (and his sample shows), unless you are using DataBinding
        (you set the ListControl.Dis playMember, ValueMember, & DataSource
        properties) a ListControl (ComboBox & ListBox) will use Object.ToString for
        the display text.

        As you found out Button, overrides Object.ToString to display
        "System.Windows .Forms.Button, Text: Button1" when it is called.

        Your classes are free to override Object.ToString to return meaningful text.

        You can use ComboBox.Select edItem & ListBox.Selecte dItem to return the
        actual object that is selected. ListControl.Sel ectedValue will return the
        text (ToString/ValueMember) of the selected item...

        Hope this helps
        Jay

        "Mendhak" <anonymous@disc ussions.com> wrote in message
        news:eZZtI$hpEH A.1308@TK2MSFTN GP14.phx.gbl...[color=blue]
        > Hi All,
        >
        > When you add an object to a combobox, and run the application, you'll get
        > something like this:
        >
        > System.Windows. Forms.Button, Text: Button1
        >
        > (for example).
        >
        > Is it possible to have just the text/value in the object to be displayed
        > in
        > the combobox, yet at the same time when retrieving the combobox item,
        > getting that same object back?
        >
        > In this particular case, an object of ANY type can be added to the
        > combobox.
        >
        > TIA.
        >
        >[/color]


        Comment

        • Mendhak The Frog

          #5
          Re: Retrieve info from combobox while adding objects of any type ??

          Really appreciate your help on all of this.
          Greg's example was what I needed.

          Thanks,
          Mendhak.


          "Mendhak" <anonymous@disc ussions.com> wrote in message
          news:eZZtI$hpEH A.1308@TK2MSFTN GP14.phx.gbl...[color=blue]
          > Hi All,
          >
          > When you add an object to a combobox, and run the application, you'll get
          > something like this:
          >
          > System.Windows. Forms.Button, Text: Button1
          >
          > (for example).
          >
          > Is it possible to have just the text/value in the object to be displayed[/color]
          in[color=blue]
          > the combobox, yet at the same time when retrieving the combobox item,
          > getting that same object back?
          >
          > In this particular case, an object of ANY type can be added to the[/color]
          combobox.[color=blue]
          >
          > TIA.
          >
          >[/color]


          Comment

          • Mendhak The Frog

            #6
            Re: Retrieve info from combobox while adding objects of any type ??

            Thanks to everyone who responded. I inherited the ComboBox control, and
            used Gregg's class.

            Some day when I become rich and famous, I'll pay you a trillion dollar
            royalty. :D

            Thanks,
            Mendhak.


            "Mendhak" <anonymous@disc ussions.com> wrote in message
            news:eZZtI$hpEH A.1308@TK2MSFTN GP14.phx.gbl...[color=blue]
            > Hi All,
            >
            > When you add an object to a combobox, and run the application, you'll get
            > something like this:
            >
            > System.Windows. Forms.Button, Text: Button1
            >
            > (for example).
            >
            > Is it possible to have just the text/value in the object to be displayed[/color]
            in[color=blue]
            > the combobox, yet at the same time when retrieving the combobox item,
            > getting that same object back?
            >
            > In this particular case, an object of ANY type can be added to the[/color]
            combobox.[color=blue]
            >
            > TIA.
            >
            >[/color]


            Comment

            Working...