Possible array solution

Posted on 2005-05-04
Last Modified: 2010-04-23
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.
Question by:augie0216
    1 Comment
    LVL 19

    Accepted 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

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now