• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 144
  • Last Modified:

Possible array solution

I have a form which has three textboxes (txtAircraftID,txtCarrierName, and txtFuelCharges). and a listbox lstAircraftId. What I need to have happen is once the user fills the textboxes and the menu is clicked the AircraftID appears in the listbox, once the text boxes are cleared and the aircraftID is highlighted in the listbox the aircraftID and the other entered text reappears in the proper textbox. I can get the aircraftID to appear in the textbox, and I can get the AircraftID to reappear in it's textbox but not the other entered text. I was hoping someone could help me with an array to accomplish this. Here is the code for the menu and the listbox.

  Private Sub mmuRecordSaveArrival_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mmuRecordSaveArrival.Click
        Dim newitemtext As String = txtAircraftID.Text

Private Sub AircraftID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstAircraftID.SelectedIndexChanged
        Dim sAircraftID As String = lstAircraftID.SelectedItem.ToString()
        txtAircraftID.Text = lstAircraftID.SelectedItem.ToString

I think an array is the solution but I don't know exactly how to code it.
1 Solution
OK here's a possible solution
Its the full form code, just copy paste in a new Form and run.

I have used HashTable instead of Array, it is easier to fill back the TextBoxes that way once they are emptied.

The form has a button Add, when you click it the values are added into the hashTable and the ID into the ListBox also the TextBoxes are cleared. Then when you select an ID from the ListBox all tthe values are again filled in the TextBoxes from the HashTable.

Public Class FrmArray
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TxtID As System.Windows.Forms.TextBox
    Friend WithEvents TxtName As System.Windows.Forms.TextBox
    Friend WithEvents TxtFuelCharges As System.Windows.Forms.TextBox
    Friend WithEvents LstIDs As System.Windows.Forms.ListBox
    Friend WithEvents CmdAdd As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TxtID = New System.Windows.Forms.TextBox
        Me.TxtName = New System.Windows.Forms.TextBox
        Me.TxtFuelCharges = New System.Windows.Forms.TextBox
        Me.LstIDs = New System.Windows.Forms.ListBox
        Me.CmdAdd = New System.Windows.Forms.Button
        Me.TxtID.Location = New System.Drawing.Point(184, 32)
        Me.TxtID.Name = "TxtID"
        Me.TxtID.Size = New System.Drawing.Size(112, 20)
        Me.TxtID.TabIndex = 0
        Me.TxtID.Text = ""
        Me.TxtName.Location = New System.Drawing.Point(184, 56)
        Me.TxtName.Name = "TxtName"
        Me.TxtName.Size = New System.Drawing.Size(112, 20)
        Me.TxtName.TabIndex = 1
        Me.TxtName.Text = ""
        Me.TxtFuelCharges.Location = New System.Drawing.Point(184, 80)
        Me.TxtFuelCharges.Name = "TxtFuelCharges"
        Me.TxtFuelCharges.Size = New System.Drawing.Size(112, 20)
        Me.TxtFuelCharges.TabIndex = 2
        Me.TxtFuelCharges.Text = ""
        Me.LstIDs.Location = New System.Drawing.Point(16, 32)
        Me.LstIDs.Name = "LstIDs"
        Me.LstIDs.Size = New System.Drawing.Size(112, 69)
        Me.LstIDs.TabIndex = 3
        Me.CmdAdd.Location = New System.Drawing.Point(200, 112)
        Me.CmdAdd.Name = "CmdAdd"
        Me.CmdAdd.Size = New System.Drawing.Size(72, 24)
        Me.CmdAdd.TabIndex = 4
        Me.CmdAdd.Text = "Add"
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(344, 273)
        Me.Name = "FrmArray"
        Me.Text = "FrmArray"

    End Sub

#End Region

    Private Structure Aircraft
        Dim ID As String
        Dim Name As String
        Dim FuelCharges As Integer
    End Structure

    Dim AircraftDetails As New Hashtable

    Private Sub FrmArray_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub CmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdAdd.Click
        Dim AircraftDetail As Aircraft
        AircraftDetail.ID = TxtID.Text
        AircraftDetail.Name = TxtName.Text
        AircraftDetail.FuelCharges = CType(TxtFuelCharges.Text, Integer)
        AircraftDetails.Add(AircraftDetail.ID, AircraftDetail)
    End Sub

    Private Sub ClearTextBoxes()
    End Sub

    Private Sub LstIDs_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LstIDs.SelectedIndexChanged
        Dim AircraftDetail As Aircraft
        AircraftDetail = AircraftDetails.Item(LstIDs.SelectedItem)
        TxtID.Text = AircraftDetail.ID
        TxtName.Text = AircraftDetail.Name
        TxtFuelCharges.Text = AircraftDetail.FuelCharges.ToString
    End Sub
End Class
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now