0

I'm using My.Settings to store different text for each item in combobox

It looks like this

enter image description here

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

  • you are going to want that code to execute when the user selects a new item too otherwise the previous period value is lost; save the values to variables which are stored in My.Settings when the form closes. – Ňɏssa Pøngjǣrdenlarp Sep 10 '14 at 11:01

1 Answers1

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

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
Hoh
  • 1,196
  • 1
  • 12
  • 30
  • the most important part was overseen I think, you forgot to call My.Settings.Save otherwise nothing will be committed. – Trevor Sep 10 '14 at 11:20
  • @MrCoDeXeR I am not using My.Settings.Save function anywhere in my code, and it's working as it should. – Hoh Sep 10 '14 at 11:22
  • Stop the app and restart and pull the values back. You have to call that otherwise it won't commit and the next time you run the app it won't have those values I guarantee... – Trevor Sep 10 '14 at 11:28
  • Man, if you want I can give you my Skype, and I will share my screen, so you can see that works :) – Hoh Sep 10 '14 at 11:30
  • Did you pull them back? – Trevor Sep 10 '14 at 11:36
  • Yes, I did. As I told you. – Hoh Sep 10 '14 at 11:49
  • You must also explicitly call the Save method of this wrapper class in order to persist the user settings. You usually do this in the Closing event handler of the main form. You can read more here http://msdn.microsoft.com/en-us/library/a65txexh.aspx – Trevor Sep 10 '14 at 11:57
  • So Microsoft must be lieing to us for awhile about this or you have something else no one else has, which I doubt... – Trevor Sep 10 '14 at 11:59
  • That's it. Yeah, I have that checked. – Hoh Sep 10 '14 at 12:13
  • That's why! But not everyone has that. If u uncheck it won't save. But calling it either way is good then you don't have to worry... – Trevor Sep 10 '14 at 12:15
  • Yeah, that does make sense. But, the thing is that was checked by default when I created project. Anyway, thanks for the info. – Hoh Sep 10 '14 at 12:17
  • @Hoh Where did u checked in my.settings so you dont need to call my.settings.save ?? – Anel_Hodzic Sep 10 '14 at 13:31
  • @Anel_Hodzic go to your Project Properties, than click on Application and at the middle you will see Save My.Settings on Shutdown checkbox. – Hoh Sep 10 '14 at 13:42
  • If that checkbox is enabled i don't need to call My.Settings.Save() anywhere in program ? . It will save data even when i restart computer and open application again ? – Anel_Hodzic Sep 10 '14 at 13:46
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/60975/discussion-on-answer-by-hoh-combobox-and-my-settings). – Taryn Sep 10 '14 at 13:54