Hello everyone i am getting input from the user in the form of an inputbox. The inputbox then dumps the information into an arrayList which then i want my Listbox to be populated by the items within the arrayList.

so...

userName = InputBox -> arrayList.add(userName) - > Listbox.items.add(arrayList)

Here is some of my code.

userName = InputBox("Enter Name: ")

        Dim users As ArrayList = New ArrayList()
        users.Add(userName)

        For i As Integer = 0 To users.Count

            [B]termListBox.Items.Add(users.Item(i))[/B]
        Next i

It gives me the error at the bold highlighted text. Any ideas?

Dani AI

Generated

A few practical notes that extend the thread's answers and avoid the common pitfalls seen here. The index-out-of-range error is caused by using an index greater than the highest valid position; VB collections are zero-based so the highest index is Count - 1. Rather than indexing manually, prefer a generic collection or bindable collection so the UI and data stay in sync and the loop logic is simpler.

A compact, robust pattern is to keep a bindable list at form scope and set it as the ListBox data source. Adding an item to the list updates the ListBox automatically:

' form-level field
Private users As New System.ComponentModel.BindingList(Of String)()

' on form load
termListBox.DataSource = users

' when a name is entered
users.Add(userName)

For quick one-off updates or batch updates, using the ListBox API is fine. Clear before a bulk replace, or use AddRange with an array:

termListBox.Items.Clear()
termListBox.Items.AddRange(myStringList.ToArray())

About the new-form that "came up and quit": that usually happens when the new form is disposed or the app exits. Common causes are showing a form inside a Using block and leaving it (disposing it) immediately, or closing the startup form while the application is configured to exit when the startup form closes. As noted, a modal call keeps the caller alive; as pointed out, the project shutdown setting controls whether the app ends when the startup form closes. Another safe pattern is to keep a class-level reference to the child form and call Show (or use ShowDialog for modal):

Private childForm As Form2 = Nothing
childForm = New Form2()
childForm.Show()

Also avoid creating or showing forms from background threads; UI work must happen on the UI thread (Invoke/BeginInvoke if needed). These approaches make the list population and form lifetime predictable and easier to debug.

Recommended Answers

All 6 Replies

What is the error message?

It said something about out of bounds. Does the syntax look right?

try:

For i As Integer = 0 To users.Count -1

That worked thanks :) I have another question. How can i load a form from another form? I tried form.show but the form came up and quit after a couple of seconds.

What is your code for new form?
Perhaps you close the new form after show.
You can try form.showdialog too.

Double click on the My Project. On the left hand side toward the bottom in the Shut Down Mode select "After last form closes".
If you don't then when you close the startup form the program ends.

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.