I have a form with a listbox and several textboxes. When I input data in the
textboxes and click the menu I have created the aircraftId should appear in
the listbox and link the data form the other textboxes with the aircraftId. So if I have three AircraftId's when I highlight one the data recorded when I first clicked the menu with that aircraftID populates the textboxes. I have gotten the aircraftId to appear in the listbox put can't get the textboxes to be populated. Here is the code I have:

Private Sub mmuRecordSaveArrival_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuRecordSaveArrival.Click
Dim newItemText As String = txtAircraftID.Text
Me.lstAircraftID.Items.Add(newItemText)

This form is to test a class I created, and imported that class to the main form,so everthing is declared in the class.

Dani AI

Generated

As suggested, simple arrays can work, but in VB.NET a small record class plus a collection (List(Of T) or BindingList(Of T)) is clearer and less error-prone than ReDim and manual counters. This approach keeps each aircraft’s fields together, makes the ListBox show just the ID, and lets the SelectedItem carry the full record to populate the textboxes.

' form-level
Private aircrafts As New System.ComponentModel.BindingList(Of Aircraft)

Public Class Aircraft
    Public Property AircraftID As String
    Public Property PilotName As String
    Public Property PlaneModel As String
    Public Overrides Function ToString() As String
        Return AircraftID
    End Function
End Class

' on form load
lstAircraftID.DataSource = aircrafts
lstAircraftID.DisplayMember = "AircraftID"

' on save/menu click
Dim a As New Aircraft With {
    .AircraftID = txtAircraftID.Text,
    .PilotName = txtPilot.Text,
    .PlaneModel = txtModel.Text
}
aircrafts.Add(a)

' on list selection change
Dim sel = TryCast(lstAircraftID.SelectedItem, Aircraft)
If sel IsNot Nothing Then
    txtAircraftID.Text = sel.AircraftID
    txtPilot.Text = sel.PilotName
    txtModel.Text = sel.PlaneModel
End If

Notes and troubleshooting: declare the collection at form level so it persists; bind the ListBox to the collection (do not mix DataSource with Items.Add); use SelectedIndexChanged (or SelectedValueChanged) and always check for Nothing to avoid exceptions; BindingList updates the ListBox automatically when items are added. For persistence between runs, serialize the list or store records in a database or file. This pattern addresses ’s “without arrays” question and avoids the ReDim/counter pitfalls from the earlier suggestions.

Recommended Answers

All 6 Replies

I would use a couple of arrays. I'd use 1 to store the ID, and however many others for each of the textbox's. So, if you have 3 text boxes, (not including the ID) you would have 4 arrays. 1 for each text box and 1 for the ID. Then, when they click the listbox, figure out which item was selected, and pull that up in the other arrays. Does, that make sense?

Could you please show me an example.

I'm not sure what changes you'll have to make for use in .NET, but the basic code would be something like this:

In The Form Level Declarations:

' /* Dimension Variables */
Dim counter
Dim newItemText() As String 
Dim pilotname() as string
Dim planetype() as string

Then in your form load:

' /* Set counter to 0 (start of array) */
counter = 0

And In Your Click Event (Or wherever):

' /* Set The Indices Of The Array's To The Value Of counter */
redim newItemText(counter)
redim pilotname(counter)
redim planetype(counter)

' /* Set The Array's Current Indice To The Values In The Textbox's */
newItemText(counter) = txtAircraftID.Text
pilotname(counter) = text1.text
planetype(counter) = text2.text

' /* Add The Item To The ListBox */
Me.lstAircraftID.Items.Add(newItemText(counter))

' /* Increment (add one to) The Counter Variable (So We Don't Over-Write Our Array) */
counter = counter + 1

Then, Whenever You click inside the listbox

' /* Set The Values Of The Textbox's To The Array's Indices Of The Index Selected IN The listbox */
txtaircraftID.Text = newItemText(lstAircraftID.SelectedIndex)
text1.text = pilotname(lstAircraftID.SelectedIndex) 
text2.text = planetype(lstAircraftID.SelectedIndex)

Or Something Like that. Let me know if that works for you.

I'm very new at vb.net your explanation was kind of confusing to me. Do you think there is a way to do it without using an array?

Not that I know of.... Unless You are storing these in a file.

Thanks for your help I'll keep trying to figure it out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.