[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
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.
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". :(