user1570048
user1570048

Reputation: 880

vb.net looping through controls and my.settings variables

i have 20 text boxes and 20 my.settings variables

textboxes and my.settings variables have the same names textbox name: mid0 and it has the value of my.settings.mid0

the text boxes are created in runtime and i have a save button and that will save the values of the text boxes in my.settings variables

here is the code

Dim Ctl As Control
    Dim MyTextbox As TextBox
    For i As Integer = 0 To 20
        Ctl = tabc.TabPages(6).Controls.Item("mid" & i.ToString)
        If Ctl IsNot Nothing Then
            MyTextbox = CType(Ctl, TextBox)
            My.Settings.("mid" & i.ToString) = MyTextbox.Text 'this line is the problem
        End If
    Next

so how do i loop through the my.settings variables? which goes form mid0 to mid20

i tried

CallByName(My.Settings, "mid" & i.ToString, CallType.Set) = MyTextbox.Text

but i get an error "Expression is a value and therefore cannot be the target of an assignment."

Upvotes: 2

Views: 1838

Answers (2)

Jet
Jet

Reputation: 526

You can't write CallByName(...)=smtg. Because CallByName is a function, not property.
But the 4th parameter of CallByName let's you do all what you want.
Here's the code you need which will work without changing all your code:

CallByName(My.Settings, "mid" & i.ToString, CallType.Set, MyTextbox.Text)

I think this will help you very much. Good Luck !

Upvotes: 1

Jim O'Neil
Jim O'Neil

Reputation: 23764

change the My.Settings line to

My.Settings("mid" & i.ToString) = MyTextbox.Text 

which really amounts to removing the period :)

Upvotes: 3

Related Questions