-1

The program is simple, it asks for the first and last name of the user along with three other interactive user input elements. When the user types in the first and last name it should use an array and send the name to a ComboBox via a click of a button. At the moment I don't know how to send the user typed values to the Combox using an array.

Below is the code I have right now:

Public Class Form1

    Dim people(20) As String
    Dim peopleIndex As Integer
    Dim firstName As String
    Dim lastName As String
    Dim blnGroup As Boolean
    Dim numberOfAdult As Integer
    Dim numberOfChild As Integer

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        peopleIndex = -1
    End Sub

    Private Sub btnAddCustomer_Click(sender As System.Object, e As System.EventArgs) Handles btnAddCustomer.Click
        peopleIndex += 1
        If peopleIndex > UBound(people) Then
            ReDim Preserve people(peopleIndex + 10)
        End If
    End Sub
End Class
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Vish
  • 3
  • 4

2 Answers2

1

Not too sure if this is what you want but it might at least give you an idea of how to use an array.

So first of all, declare a global list of string like this:

Dim people As New List(Of String)

Then add a combobox, a textbox and another button to your form.

Now try this:

Private Sub btnAddCustomer_Click(sender As Object, e As EventArgs) Handles btnAddCustomer.Click

    '' Add the textbox1's text to the list of strings and to the combobox
    people.Add(TextBox1.Text)
    ComboBox1.Items.Add(TextBox1.Text)

End Sub

Private Sub btnDelCustomer_Click(sender As Object, e As EventArgs) Handles btnDelCustomer.Click

    '' Remove the selected item from the list of strings
    people.Remove(ComboBox1.SelectedItem)
    '' After delete, repopulate the combobox
    ComboBox1.Items.Clear()
    For Each person In people
        ComboBox1.Items.Add(person)
    Next

End Sub

Make sure you name the second button to 'btnDelCustomer' or just change the event handler. Again, I'm not really clear on what you want so if you need more information, just ask.

Jordan
  • 323
  • 1
  • 14
  • This looks promising, I will try it out and message you. – Vish May 11 '15 at 20:27
  • This works, unfortunately I already happen to know how to print something from text boxes on to ComboBox, however what I'm really after is how to do it via using an array, because I need to add different items to the comboBox and each of those items will contain different attributes, and after adding multiple items I then should be able to select each one from the comboBox and the attributes will be displaying In a label as I select the items from the comboBox. – Vish May 11 '15 at 20:42
  • An example of what I'm trying to accomplish will be: Suppose it is a booking system; Adding a customer to the Combox (Only the name), other information about the customer will be the Sex, Age and Phone number, these will be stored in a variable and after adding the customer to the ComboBox I should be able to select them and see the Sex, age and Phone Number appear as a label on the form. – Vish May 11 '15 at 20:47
1

Ok, how about this:

First declare your global array lists.

Dim people, age, sex, phoneNo As New ArrayList

Now for your add button, use this:

    '' check to see If all text fields have a user input
    If Not ((TextBox1.Text = "") Or (TextBox2.Text = "") Or (TextBox3.Text = "") Or (TextBox4.Text = "")) Then
        people.Add(TextBox1.Text)
        ComboBox1.Items.Add(TextBox1.Text)
        TextBox1.Text = Nothing
        age.Add(TextBox2.Text)
        TextBox2.Text = Nothing
        sex.Add(TextBox3.Text)
        TextBox3.Text = Nothing
        phoneNo.Add(TextBox4.Text)
        TextBox4.Text = Nothing
    Else
        MessageBox.Show("Please fill in all fields.")
    End If

For your combo box - selected index changed, use this:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged

    '' When the combobox has changed its value and is not -1, update the labels
    If Not ComboBox1.SelectedIndex = -1 Then
        Label1.Text = String.Format("Age: {0}", age(ComboBox1.SelectedIndex))
        Label2.Text = String.Format("Sex: {0}", sex(ComboBox1.SelectedIndex))
        Label3.Text = String.Format("Phone Number: {0}", phoneNo(ComboBox1.SelectedIndex))
    End If

End Sub

and finally for your delete button, use this:

    If Not ComboBox1.SelectedIndex = -1 Then

        '' Remove the selected values from the array list
        people.RemoveAt(ComboBox1.SelectedIndex)
        age.RemoveAt(ComboBox1.SelectedIndex)
        sex.RemoveAt(ComboBox1.SelectedIndex)
        phoneNo.RemoveAt(ComboBox1.SelectedIndex)

        '' Repopulate the combobox
        ComboBox1.Items.Clear()
        For Each person In people
            ComboBox1.Items.Add(person)
        Next

        '' Update the labels
        Label1.Text = "Age: None Selected!"
        Label2.Text = "Sex: None Selected!"
        Label3.Text = "Phone Number: None Selected!"
    Else
        MessageBox.Show("None Selected!")
    End If

Obviously this is just a mock up and you would take this and impliment it in your own way. Hope this helps

Jordan
  • 323
  • 1
  • 14
  • Okay this is good thanks! You helped me do 80% of what I'm trying to achieve. But do you know how to select the added items so that the user can see what the booking contains. Need to be able to do this for multiple pre-added items. – Vish May 13 '15 at 11:52
  • Umm, I'm not too sure what you mean here but if you change the combo box selection, it will load the information into the labels, from the array list's. Also, please could you mark this as the right answer if this is what you wanted? Thanks – Jordan May 13 '15 at 19:43
  • Oh wow, iv noticed i have copied the same code into two different places. I will update it. – Jordan May 13 '15 at 19:45
  • --READ ME-- Ok so i have updated my answer because i had actually pasted in the wrong piece of code for the middle one (combo box). I have updated it now and you should find that changing the selected item in the combo box, updates the labels and fills them with the stored information about the person – Jordan May 13 '15 at 20:45