Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Navigating through an array

Status
Not open for further replies.

hugh999

MIS
Nov 29, 2001
129
IE
Hi

I have an array that contains a list of elements that I can export to a list box using (For Each), but what I am trying to achieve is to retrieve the elements 1 by 1 and add the elements to a text box.

For example, when my form loads, the first element of the array will show in the text box. What I would like is to have a Next button that when clicked will show the second element to the text box and if I click on the Back button I will get the previous element to the text box.

Any code examples would be appreciated.

Thanks
 
I did it like this:

Code:
    Private aItems() As String = {"One", "Two", "Monkey", "Cat"}

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

        'Place First Value In TextBox
        Me.TextBox1.Text = aItems(0)

    End Sub

    'Get Previous Value
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'Find Index Of Current Value And Subtract 1
        Dim i As Int32 = Array.IndexOf(aItems, Me.TextBox1.Text) - 1

        'If The New Index Is Less Than Zero, Get The Last Value In The List.  This Allows "Looping"
        If i < 0 Then
            i = i + aItems.Length
        End If

        'Put The New Value In The TextBox
        Me.TextBox1.Text = aItems(i)

    End Sub

    'Get Next Value
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        'Find Index Of Current Value And Add 1
        Dim i As Int32 = Array.IndexOf(aItems, Me.TextBox1.Text) + 1

        'If The New Index Is Greater Than List Length, Get The First Value In The List.  This Allows "Looping"
        If i = aItems.Length Then
            i = i - aItems.Length
        End If

        'Put The New Value In The TextBox
        Me.TextBox1.Text = aItems(i)

    End Sub

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Thanks for the code. I tried it in with the code i have, but unfortunately it did not work, I think it is to do with the type of array that I have.

Below is the code that I have so far. Button 1 adds info from textbox1 and textbox2 to the array and button 2 adds the elements from the array to the listbox

Code:
 Dim contact_list As New ArrayList
 Dim currentID As Integer

Private Function CreateNewRecord() As Integer
        Dim p As New Person

        CreateNewRecord = contact_list.Count + 1
      
        p.ID = contact_list.Count + 1
    
        contact_list.Add(p)
End Function

Private Function UpdateRecord(ByVal currentID As Integer, ByVal Name As String, ByVal Phone As String)
      Dim p As New Person

        For Each p In contact_list
            If p.ID = currentID Then
                Exit For
            End If
        Next
     
        p.Name = Name
        p.Phone = Phone
End Function

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        currentID = CreateNewRecord()
        UpdateRecord(currentID, TextBox1.Text, TextBox2.Text)

        TextBox1.Text = ""
        TextBox2.Text = ""

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim p As New Person
      
        For Each p In contact_list
           
            ListBox1.Items.Add(p.Name & p.Phone)
          
        Next

End Sub
 
Hugh,

I am confused. Your first post indicated you wanted to get the previous/next value out of an array and put it into a text box. But your second post shows you are working with ArrayList and ListBox. As well as more than one text box, etc. These are not the same.

Perhaps you can start again, ignoring what you have and describe the setup you are looking for. Also, include some sample data if you can. And also maybe how you are collecting the data. I notice you seem to be using a class (Person), so a description of what that is will help as well.

The code I provided I pulled directly off my test form I created for your original described scenario. I had one text box and two buttons. I defined a static array just for testing purposes and walked back and forth through it. The code worked according to original post.

Always glad to help.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Sorry for the confusion. My VB.Net experience is limited.

The code array that I am working with is old code that I am trying to change to achieve what is in my original post.

The reasoning for the array is, I have a small quiz game that extracts questions and the question number from an XML file. When the form loads the first question and question number is displayed on the form, when the user clicks the Next button the next questions and question number is displayed and clicking Back shows the previous question and question number. When the user clicks on the Answer button they are informed if there answer is right or wrong

What I am aiming to do is when the user types the answer to a question and clicks the Answer button the question number (from a textbox) gets added to the array and also the text “Yes” if they have answered correctly, “No” if they answered incorrectly or “none” if the question was not answered. The “Yes”, “No” and “none” text will be used to display an image to each question indicating the status (Right or Wrong or not answered). I would like to use the Next and Back buttons to navigate through the array so question 1 will be associated with the first element, question 2 will be associated with second element and so on.

In the array code that I have I can change to fit in with the game but I need the code to navigate up and down the elements.

In the code that you supplied, can the array be changed to accept any number of elements and can I use a textbox to add a question number and second textbox to add “Yes”, “No” and “none” to the array.

I appreciate your help with this.
 
I don't see why my supplied example couldn't be modified to allow jagged arrays. But that is not something I have much experience in. I tend to just create several arrays of the same upperbound and fill each one...one for questions, one for answers, one for etc as needed. That might also be abit more intuitive for you at this point.

=======================================
People think it must be fun to be a super genius, but they don't realize how hard it is to put up with all the idiots in the world. (Calvin from Calvin And Hobbs)

Robert L. Johnson III
CCNA, CCDA, MCSA, CNA, Net+, A+, CHDP
VB/Access Programmer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top