Results 1 to 3 of 3

Thread: [2005] Question on adding to listbox from an object

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    97

    [2005] Question on adding to listbox from an object

    I have a form that contains textboxes and a few radio buttons and i want to pass the values of the controls to a method in my class CAirPort. It does this fine except I want the listbox on my frmMain to show the ID (which is a string) of the item added inside the box instead. Just not sure how to access variables of objects inside of the arraylist that my objects go into when created. Below is my code any help appreciated, just need to display the ID of the object in the listbox.

    Code:
     Public myAirPort As New CAirport
    
    
     Private Sub btnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRecord.Click
            planeID = txtID.Text
            carrierName = cboxCarrier.Text
            Call myAirPort.Arrival(fuelCharge, planeDeparture, planeArrival, carrierName, planeType, planeID)
    
            frmMain.lstAirplanes.Items.Add(myAirPort)
    
            MessageBox.Show("Arrival Recorded")
            Me.Close()
        End Sub
    Code from CAirPort Class
    Code:
     Private mAirplanes As New ArrayList()
    
      Public Sub Arrival(ByVal planeFuelCharge As Decimal, ByVal planeDeparture As Date, ByVal planeArrival As Date, ByVal planeCarrierName As String, _
        ByVal planeType As typeAirPlane, ByVal planeID As String)
    
            mAirplanes.Add(New CAirplane(planeFuelCharge, planeDeparture, planeArrival, planeCarrierName, planeType, planeID))
    
        End Sub
    Last edited by Planet_x; Apr 12th, 2008 at 11:47 PM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] Question on adding to listbox from an object

    In your CAirport class, override the ToString method and have it return the ID of the object as a string.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Question on adding to listbox from an object

    Don't add the CAirport objects to the ListBox itself. Add them to a collection bound to the ListBox. Then you simply set the ListBox's DisplayMember to "ID". If you use a BindingList(Of CAirport) then the ListBox will update automatically when you add or remove items.

    Isn't "CAirport" a bit old-school C/C++? Are you adding them to a CListBox? Do you forget that the ListBox is a class without the "C" prefix? The irony is, I just started work recently at a shop that use Hungarian as standard. They've still got a lot of code where the classes are prefixed with a "C" although the boss said we don't have to do that any more. I get heartburn every time I type "lintIndex".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width