# Visual Basic > Visual Basic .NET > VS 2022 Save all textbox values into one save

## Kicker95

Hi!

I have a form that has about 100 textboxes and people gonna enter numbers in it. I want textbox values to be saved everytime you exit and load application. 

I know that I can create my.settings for each textbox but we are talking about 100 textboxes. Is there no easy way? Do i have to create setting for each textbox to save them? Can I create one setting for all textboxes or use "dim statement" or for "each control statement" to save them all?

does it have to look like this?  :Big Grin:

----------


## techgnome

>> I know that I can create my.settings for each textbox but we are talking about 100 textboxes. Is there no easy way? 
Sure there is...

>> Do i have to create setting for each textbox to save them? 
Nope. You can, but you don't have to.

>> Can I create one setting for all textboxes or use "dim statement" or for "each control statement" to save them all?
Yep. You sure can.

>> does it have to look like this?
Nope, it does not.

Look up User Config and string lists ... there's a specific type that you can use to work with a collection of strings ... Specialized something or other.

-tg

----------


## jmcilhinney

The type tg is alluding to is System.Collections.Specialized.StringCollection. You can add a setting of that type and it will be Nothing by default. If you want default values for your TextBoxes, edit the setting and add the values. In your code, you can do something like this to load the data:

vb.net Code:
If My.Settings.SomeListSetting Is Nothing Then
    'This will happen the very first time the app is run if you don't edit the setting value.
    My.Settings.SomeListSetting = New Specialized.StringCollection
Else
    Dim textBoxes = Controls.OfType(Of TextBox)().ToArray()
    Dim values = My.Settings.SomeListSetting
     For i = 0 To textBoxes.GetUpperBound(0)
        textBoxes(i).Text = values(i)
    Next
End If
You do basically the opposite to save the data:

vb.net Code:
Dim textBoxes = Controls.OfType(Of TextBox)().ToArray()
Dim values = My.Settings.SomeListSetting
 values.Clear()
 For i = 0 To textBoxes.GetUpperBound(0)
    values(i) = textBoxes(i).Text
Next
Note that this code assumes that those TextBoxes have been added to the form directly and that they are the only TextBoxes on the form. If you have other TextBoxes on the form then you should add a container, e.g. a Panel, and then move those TextBoxes to it. If the TextBoxes are in another container then you must use the Controls collection of that container rather than the form. This all ensures that the OfType method will get all the correct TextBoxes and only the correct TextBoxes.

----------


## vbdotnut

> If the TextBoxes are in another container then you must use the Controls collection of that container rather than the form. This all ensures that the OfType method will get all the correct TextBoxes and only the correct TextBoxes.


 You're holding back on him JM, I am picturing some poor soul reading this some day and manually creating 100 textbox's

----------


## Kicker95

Thank you for replying. Have been testing this code all day now and trying find what im doing wrong. Got same error on my original project so I created a new project, a clean form with just two textboxes to test it but I get same error:

Index was out of range. Must be non-negative and less than the size of the collection.

Attachment 186478Attachment 186479

----------


## Kicker95

> >> I know that I can create my.settings for each textbox but we are talking about 100 textboxes. Is there no easy way? 
> Sure there is...
> 
> >> Do i have to create setting for each textbox to save them? 
> Nope. You can, but you don't have to.
> 
> >> Can I create one setting for all textboxes or use "dim statement" or for "each control statement" to save them all?
> Yep. You sure can.
> 
> ...


I know why you answer like this and I appreciate the way you do it.

----------


## wes4dbt

The attachment doesn't work.

Just post the relevant code.  Between code tags "#".

----------


## Kicker95

> The attachment doesn't work.
> 
> Just post the relevant code.  Between code tags "#".




```

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If My.Settings.save1 Is Nothing Then
            My.Settings.save1 = New Specialized.StringCollection
        Else
            Dim textBoxes = Controls.OfType(Of TextBox)().ToArray()
            Dim values = My.Settings.save1

            For i = 0 To textBoxes.GetUpperBound(0)
                textBoxes(i).Text = values(i)
            Next
        End If
    End Sub

    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
        Dim textBoxes = Controls.OfType(Of TextBox)().ToArray()
        Dim values = My.Settings.save1

        values.Clear()

        For i = 0 To textBoxes.GetUpperBound(0)
            values(i) = textBoxes(i).Text
        Next
    End Sub
End Class
```

values(i) = textBoxes(i).Text: System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Arg_ParamName_Name'

the error comes up when I close the application.

----------


## jmcilhinney

If the form is already closed then there are no TextBoxes so you can't get any data from them. Put that code in the FormClosing event handler instead, which is raised before the form is closed, not after.

Also, 'save1' is terrible name for that setting. How would anyone have any idea what that was for without it being explained to them? ALL names should be descriptive. For one thing, arrays and collections should have pluralised names to indicate that they contain multiple values.

----------


## Kicker95

> If the form is already closed then there are no TextBoxes so you can't get any data from them. Put that code in the FormClosing event handler instead, which is raised before the form is closed, not after.
> 
> Also, 'save1' is terrible name for that setting. How would anyone have any idea what that was for without it being explained to them? ALL names should be descriptive. For one thing, arrays and collections should have pluralised names to indicate that they contain multiple values.


still same with formclosing. Its same error and it happens when I close form with X not when I write inside textbox. It does not crash until I close app.



```
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        If My.Settings.save1 Is Nothing Then
            My.Settings.save1 = New Specialized.StringCollection
        Else
            Dim textBoxes = Controls.OfType(Of TextBox)().ToArray()
            Dim values = My.Settings.save1

            For i = 0 To textBoxes.GetUpperBound(0)
                textBoxes(i).Text = values(i)
            Next
        End If
    End Sub

    Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed

    End Sub

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        Dim textBoxes = Controls.OfType(Of TextBox)().ToArray()
        Dim values = My.Settings.save1

        values.Clear()

        For i = 0 To textBoxes.GetUpperBound(0)
            values(i) = textBoxes(i).Text
        Next
    End Sub
End Class
```

----------


## jmcilhinney

Oops! Sorry, my mistake. This:

vb.net Code:
values(i) = textBoxes(i).Text
should have been this:

vb.net Code:
values.Add(textBoxes(i).Text)
The original line would have worked if the collection was initialised in the Settings properties and the Clear method wasn't called, which is what I had done initially. Because the collection is empty though, you can't set the item at index i because there are no items at any index.

----------


## Kicker95

> Oops! Sorry, my mistake. This:
> 
> vb.net Code:
> values(i) = textBoxes(i).Text
> should have been this:
> 
> vb.net Code:
> values.Add(textBoxes(i).Text)
> The original line would have worked if the collection was initialised in the Settings properties and the Clear method wasn't called, which is what I had done initially. Because the collection is empty though, you can't set the item at index i because there are no items at any index.


Thank you, works now  :Big Grin:

----------

