views:

1069

answers:

8

Hi, I am using My.Settings in visual studio 2008 to store information, for when the user runs the program again.

I have that working fine... but as I am using 12 textboxes I don't want to write...

my.settings.grade1 = textbox1.text

for each one, and I am also making calculations using the stored information, so I dont want to be writing my.settings.grade1 + my.settings.grade2 etc..

Any help welcome

Thanks =)

A: 

I havent written much... I have tried this loop

For i = 1 To 12

        With My.Settings

            .Grade(i) = grade(i).Text

End With
    Next

But that obv doesnt work....

Thanks

For future reference this should be an edit to your question so that people coming later don't miss it.
Bryan Anderson
A: 

There must be a easier way than using reflection?

You are not on a forum. Use the "add comment" link to answer people.
PhiLho
+1  A: 

On your form that has the textboxes, add them to a collection or an array of textboxes when the form is initialised.

Then, iterate through the collection or array of textboxes to assign to the setting value.

If you don't want to manually code the assignment of the textboxes into the array, then in your form initialisation code, iterate through all controls on the form and check for the control type or a specfici 'Tag' you assign to each textbox, then add each textbox to the array that way.

For Each c as Control in Me.Controls

 If c.Tag.ToString() = "Grade" Then
  ' Add Items to collection here '
 End If

Next c
Jayden
A: 

Do you mean something like?

    Dim sum As Long
    Dim grades(11) As Long

    Dim i As Integer = 0
    For Each ctr In Controls
        If TypeOf (ctr) Is TextBox Then
            grades(i) = CLng(ctr.Text)
            sum = sum + grades(i)
            i = i + 1
        End If
    Next
Jim Anderson
how do you know the controls are in the same order as the data? You dont want to loop through every control in the form as you may have controls that dont need to be assigned values.
Victor
The OP didn't specify details. I was just trying to present a possible method - the actual code would have to tailored to the specific requirements.
Jim Anderson
If there is anything other than a grades textbox in there, your indexes would end up out of bounds.
Joel Coehoorn
A: 

or you could do something like this:

given the your textboxes are named along the lines of: Grade1, Grade2, Grade3, etc.

you could store the Grades in an Array and then loop through the array:

((TextBox)form.findControl("Grade" + i.ToString())).Text = Grade(i)

Depending on your calculation, then you could also execute the calculation inside the loop.

Victor
Yea, that is better than my idea (if the name of the textbox contains the index of the grade).
Jim Anderson
A: 

I am not using an array to store the grades entered

My.Settings.Grade1 = grade1.Text My.Settings.Grade2 = grade2.Text

etc.....They are being stored using the visual studio function my.settings."storeage name"

I can loop thought the textboxes fine... but using the my.settings is diffcult..

I think using an array (or other list/collection) would make more sense than have 12 variables names grade1...grade12. You are limiting what you can do by your design.
Jim Anderson
the point of using my.settings.grade... is so I can load this information once the program is closed. my.settings.reload and my.settings.save. Its like a database but easier
It's not that you're using My.settings: it's that you're putting individual values into My.Settings rather than an array or List.
Joel Coehoorn
A: 

Populate a List of grade textboxes:

'at the class level'
Public GradeBoxes(11) As TextBox
Const grade As String = "GRADE"

'when the form is created'
Dim i As Integer = 0
For Each ctr As Control In Controls
    If TypeOf (ctr) Is TextBox AndAlso ctr.Name.ToUpper.StartsWith(grade) Then
        i = CInt(ctr.Name.SubString(grade.Length))
        If i >= 0 AndAlso i < GradeBoxes.Length Then GradeBoxes(i) = ctrl
    End If
Next ctr

For Each box As TextBox in GradeBoxes
    If box IsNot Nothing AndAlso My.Settings(box.Name) IsNot Nothing Then
        box.Text = My.Settings(box.Name)
    End If
Next box

Save grades:

For Each box As TextBox in GradeBoxes
    If box IsNot Nothing AndAlso My.Settings(box.Name) IsNot Nothing Then
        My.Settings(box.Name) = box.Text
    End If
Next box
My.Settings.Save()
Joel Coehoorn
+1  A: 

Have you considered using ApplicationSettings Binding to automatically bind your values to your Textboxes.Text properties. This will support 2-way binding and then all you have to do is Call Save when you close.

bendewey