Combobox和My.Settings

时间:2014-09-10 10:23:12

标签: vb.net text combobox my.settings

我使用My.Settings为组合框中的每个项目存储不同的文字

看起来像这样

enter image description here

我在项目设置中添加了5个类型为" String"

的设置

我正在使用此代码

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If cmbperiod.SelectedItem = 1 Then
        txtnote.Text = My.Settings.first()
    ElseIf cmbperiod.SelectedItem = 2 Then
        txtnote.Text = My.Settings.second()
    ElseIf cmbperiod.SelectedItem = 3 Then
        txtnote.Text = My.Settings.third()
    ElseIf cmbperiod.SelectedItem = 4 Then
        txtnote.Text = My.Settings.fourth()
    ElseIf cmbperiod.SelectedItem = 5 Then
        txtnote.Text = My.Settings.fifth()
    End If
End Sub

我可以从my.settings加载值但是如何编辑它。

示例我想键入第三期的注释,并在Form_Closing上将该文本保存到我的设置中。第三次,所以下次打开表单时,它将被加载。

阿奈尔

1 个答案:

答案 0 :(得分:1)

  Private Sub Form_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
        If cmbperiod.SelectedItem = 1 Then
            My.Settings.first() = txtnote.Text
        ElseIf cmbperiod.SelectedItem = 2 Then
            My.Settings.second() = txtnote.Text
        ElseIf cmbperiod.SelectedItem = 3 Then
            My.Settings.third() = txtnote.Text
        ElseIf cmbperiod.SelectedItem = 4 Then
            My.Settings.fourth() = txtnote.Text
        ElseIf cmbperiod.SelectedItem = 5 Then
            My.Settings.fifth() = txtnote.Text
        End If
    End Sub

这将解决您的问题,但只保存当前选定的值 因此,例如,如果要保存多个值,则必须添加txtnote.leave之类的命令,以捕获当前更改。
例如:

Private Sub txtnote_Leave(sender As Object, e As EventArgs) Handles txtnote.Leave
        If cmbperiod.SelectedItem = 1 Then
            My.Settings.first() = txtnote.Text
        ElseIf cmbperiod.SelectedItem = 2 Then
            My.Settings.second() = txtnote.Text
        ElseIf cmbperiod.SelectedItem = 3 Then
            My.Settings.third() = txtnote.Text
        ElseIf cmbperiod.SelectedItem = 4 Then
            My.Settings.fourth() = txtnote.Text
        ElseIf cmbperiod.SelectedItem = 5 Then
            My.Settings.fifth() = txtnote.Text
        End If
    End Sub

在此之后,您可能希望执行SelectedIndexChanged,因此当您选择不同的ComboBox值时,您也可以加载txtnote个值。
您将以这种方式执行此操作:

Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        If cmbperiod.SelectedItem = 1 Then
            txtnote.Text = My.Settings.first()
        ElseIf cmbperiod.SelectedItem = 2 Then
            txtnote.Text = My.Settings.second()
        ElseIf cmbperiod.SelectedItem = 3 Then
            txtnote.Text = My.Settings.third()
        ElseIf cmbperiod.SelectedItem = 4 Then
            txtnote.Text = My.Settings.fourth()
        ElseIf cmbperiod.SelectedItem = 5 Then
            txtnote.Text = My.Settings.fifth()
        End If
    End Sub