在Visual Basic中将所有my.settings加载到文本框中

时间:2016-01-21 06:49:38

标签: vb.net loops controls my.settings

我正在尝试将所有已保存的my.settings加载到我的文本框中,但我无法检索已保存的值。这是我的代码

Dim ctrl As Control
    For Each ctrl In Me.Controls
        If (ctrl.GetType() Is GetType(TextBox)) Then
            Dim txt As TextBox = CType(ctrl, TextBox)
            For i As Integer = 1 To 20
                txt.Text = My.Settings("fp" & i)
            Next
        End If
    Next

这样做的正确方法是什么?谢谢

1 个答案:

答案 0 :(得分:0)

通常,当您引用存储在设置中的值时,它将沿着;

My.Settings.<name of the setting>

My.Settings有一个Item属性,它将Settings PropertyName(作为字符串)作为参数,允许您设置或获取相关值。

首先尝试以下内容;

Dim ctrl As Control
    For Each ctrl In Me.Controls
        If (ctrl.GetType() Is GetType(TextBox)) Then
            Dim txt As TextBox = CType(ctrl, TextBox)
            For i As Integer = 1 To 20
                txt.Text = My.Settings.Item("fp" & i.ToString)
            Next
        End If
    Next