How do I save items from several text box fields to an array and have that array print to a list box when I click a button?

I have 4 text box fields that get updated frequently.  When I click my button save, I want the text in each field to get saved in an array as a string.  I only want to save about 10 instances of the items that appeared in each field in the array.  I also want to be able to display the items in the array in a list box when I click a button.  
newtt13_newtt13Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Not sure what you're asking for here...

Maybe something like?
Public Class Form1

    Private SavedData As New List(Of MyData)

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim md As New MyData
        md.Data.Add(TextBox1.Text)
        md.Data.Add(TextBox2.Text)
        md.Data.Add(TextBox3.Text)
        md.Data.Add(TextBox4.Text)
        SavedData.Add(md)

        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox1.Focus()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        ListBox1.Items.Clear()
        ListBox1.Items.AddRange(SavedData.ToArray)
    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Enter AndAlso TypeOf Me.ActiveControl Is TextBox Then
            SendKeys.Send("{TAB}")
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex <> -1 Then
            Dim md As MyData = DirectCast(ListBox1.SelectedItem, MyData)
            Debug.Print("Data:")
            For Each item As String In md.Data
                Debug.Print(item)
            Next
        End If
    End Sub

End Class

Public Class MyData

    Public Data As New List(Of String)

    Public Overrides Function ToString() As String
        Return String.Join(", ", Data.ToArray)
    End Function

End Class

Open in new window

0
 
RainverseCommented:
You might try something like this: (disclaimer: Untested code...)

Public Structure stTextBoxText
        Public text1 As String
        Public text2 As String
        Public text3 As String
        Public text4 As String
 End Structure
    
Private arrTextBoxText As stTextBoxText()

if  arrTextBoxText.Length <= 10 Then
    Dim currText As New stTextBoxText
    Dim newSize as Integer = arrTextBoxText.Length+1
    ReDim Preserve arrTextBoxText(newSize)
    stTextBoxText.text1 = txtBox1.Text
    stTextBoxText.text2 =  txtBox2.Text
    stTextBoxText.text3 =  txtBox3.Text
    stTextBoxText.text4 =  txtBox4.Text
    arrTextBoxText(newSize) = stTextBoxText
End If

Open in new window


Then to display simply loop through the array and structures

-MJC
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Not sure I quite understand what you're after...is this close?
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim md As New MyData
        md.Data.Add(TextBox1.Text)
        md.Data.Add(TextBox2.Text)
        md.Data.Add(TextBox3.Text)
        md.Data.Add(TextBox4.Text)
        ListBox1.Items.Add(md)

        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()

        TextBox1.Focus()
    End Sub

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
        If keyData = Keys.Enter AndAlso TypeOf Me.ActiveControl Is TextBox Then
            SendKeys.Send("{TAB}")
            Return True
        End If
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        If ListBox1.SelectedIndex <> -1 Then
            Dim md As MyData = DirectCast(ListBox1.SelectedItem, MyData)
            Debug.Print("Data:")
            For Each item As String In md.Data
                Debug.Print(item)
            Next
        End If
    End Sub

End Class

Public Class MyData

    Public Data As New List(Of String)

    Public Overrides Function ToString() As String
        Return String.Join(", ", Data.ToArray)
    End Function

End Class

Open in new window

Idle-Mind-512867.flv
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
RainverseCommented:
Wow.... /bow to superiority... Go with his.  Way better.  

-MJC
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Lol..."better" is relative.  Just depends what you're doing!  =)
0
 
newtt13_newtt13Author Commented:
In your code, you were able to click a button and all the text fields were added to the listbox from the text fields directly.  How do I manange to save the text fields into an array with a button that i named "save"?  then, how do I click a button that i named "print" and display the information into the listbox from the array?  i guess that is where I am getting confused.
0
 
newtt13_newtt13Author Commented:
Fantastic solution.  I was able to implement the code into my environment with ease.
0
All Courses

From novice to tech pro — start learning today.