Question Dynamically Change Combo Box and Save Changes

mla_ca520

Member
Joined
Jul 30, 2009
Messages
8
Location
New Mexico
Programming Experience
Beginner
Hello, the following code will update the combobox list with new entries and even move repeated entries to the top of the list. I want to save the list so that the next time a user opens the program, the previous list appears in the combobox. Not sure how to accomplish that.
Thanks in advance for any help you can give me.

Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
're-declare "strComputer" to get value from combobox "combComputer"
Dim strComputer As String = combComputer.Text
'check if value is in list, if not then add it at the top
If Not combComputer.Items.Contains(strComputer) Then
combComputer.Items.Insert(0, strComputer)
combComputer.SelectedIndex = 0
Else
'value was in list, delete it and add at top of list
combComputer.Items.Remove(strComputer)
combComputer.Items.Insert(0, strComputer)
combComputer.SelectedIndex = 0
End If
'update label with status of remote computer
statusLBL()
End Sub
 
Answer

'either dbl click "My Project" or right click 'project name' and select properties
'click the "settings" tab I named them com0 - com7
'one setting for each combo box entry
'the following code will populate the combo box (as pername of sub routine)
Private Sub PopulateComboBox()
Dim n As Integer = 0
'populate dropdown list with saved settings: my.settings(ComNam(0-7))
For n = 0 To 7
If My.Settings.Item("ComNam" & n) <> Nothing Then
combComputer.Items.Insert(n, My.Settings.Item("ComNam" & n))
End If
Next

End Sub


'the following code will save the combo box entries when app closes
'provided you run 'Save_Settings()' on close event for app
Private Sub Save_Settings()
Dim n As Integer = combComputer.Items.Count

'there are items in list...run the loop to populate my.items.(ComNam(0-7))
If n <> 0 Then
For n = 0 To (n - 1)
Select Case n
Case n
If Not combComputer.Items.Item(n) = Nothing Then
My.Settings.Item("ComNam" & n) = combComputer.Items.Item(n)
End If
End Select
Next
End If
'save the settings. They will populate drop down list on application launch
My.Settings.Save()
End Sub


'the following code will clear the combobox and erase the history
Private Sub btnClear_combComputer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles btnClear_combComputer.Click
Dim msgBox_Rslt As DialogResult
msgBox_Rslt = MessageBox.Show("Are you sure that" & Chr(13) & "You want to ERASE" & _
Chr(13) & "The Drop Down List?", "Clear Drop Down List?", MessageBoxButtons.YesNo)
Select Case msgBox_Rslt
Case Windows.Forms.DialogResult.Yes
Dim n As Integer = 0
For n = 0 To 7 'clears my.settings.(computer names or ip's)
My.Settings.Item("ComNam" & n) = Nothing
Next
My.Settings.Save()
combComputer.Items.Clear()
combComputer.Text = Nothing
Case Windows.Forms.DialogResult.No
Exit Sub
End Select
End Sub
 
Back
Top