I'm using My.Settings to store different text for each item in combobox
It looks like this
I added to project settings 5 settings with type "String"
I'm using this code
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
I can load value from my.settings but how can i edit it.
Example i want to type the note for third period and on Form_Closing to save that text to my settings.third so next time i open form it will be loaded.
Anel
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
This will solve your question, but will save only the currently selected value.
So, for example, if you want to save multiple values, you will have to add a command like txtnote.leave
which will catch current change.
For example:
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
After this, you would probably want to perform SelectedIndexChanged
so when you select different ComboBox
values you can load txtnote
values too.
You will do it in this way:
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