I need people to be able to Add items to the ComboBox and then it will save it after adding so when it is reloaded it is still there! I just want it to save to the settings or something. No db or anything.
Printable View
I need people to be able to Add items to the ComboBox and then it will save it after adding so when it is reloaded it is still there! I just want it to save to the settings or something. No db or anything.
Hey,
Have a look at the link in my signature about My.Settings.
You should be able to use this to store the items that are contained within your ComboBox, and reload them from My.Settings when your form loads again.
Gary
Add a StringCollection to your Settings and then you can use that to store your ComboBox items. It's a collection like any other, so it has Clear and Add methods, etc.
Thinking about it, another way of doing it would be to create a class that represents the thing that you want to store, create a List(Of thing), make this serializable, and then set this as the DataSource for the ComboBox.
A general discussion about serialization can also be found in my signature.
Gary
well from what i understood, I could do this exactly like I save a textbox. so I made a button with the code:
but I get the error:vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click My.Settings.ComboBox1 = ComboBox1.Items 'and Etc... My.Settings.Save() End Sub
Oh, and the setting i did was:Code:Value of type 'System.Windows.Forms.Combobox.ObjectCollection' cannot be converted to 'System.Collections.Specialized.String.Collection'.
Incase you alrdy didnt know: I am not very good with VB ><vb Code:
Name: ComboBox1 Type: System.Collections.Specialized.StringCollection Scope: User Value:
/bump?
Hey,
The error message that you are getting is pretty self explanatory.
You have a System.Collections.Specialized.StringCollection, your My.Setting to hold the items, and you are trying to put a System.Windows.Forms.Combobox.ObjectCollection into it. There is no direct conversion possible between these two types.
Instead, you are going to have to do something like this when you save:
And this when you load them back up again:Code:Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If My.Settings.MyComboItems Is Nothing Then
My.Settings.MyComboItems = New System.Collections.Specialized.StringCollection()
End If
For Each comboItem As Object In Me.ComboBox1.Items
My.Settings.MyComboItems.Add(comboItem)
Next
My.Settings.Save()
End Sub
GaryCode:Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each comboItem As String In My.Settings.MyComboItems
Me.ComboBox1.Items.Add(comboItem)
Next
End Sub
I advise you to use an XML-file. I also used to use the Settings, but after a while I got a bit sick of the fact that the Settings is overwritten after a new build. Now I use an XML-file and it works really great.
How would I do that XML save?...im not any good at VB ><
Oh, and any way to get it to load setting on start?
Sorry if im asking more than one thing at once..Just trying to learn all I can :)
In order to get it to load on startup, put the code that I showed you in post #7 into the main form's load event.
If you are just starting out, which it sounds like you are, I would recommend that you stick with My.Settings for now, until you are a bit more comfortable.
Then you can look into creating your own XML file, using the XmlDocument class:
http://msdn.microsoft.com/en-us/libr...ldocument.aspx
Or, using custom class serialization that I mentioned earlier, and which is linked to in my signature.
Gary
Can you confirm what exactly you mean by this? Once your application is installed, the XML file that is generated by your application for My.Settings will be placed within the users documents and settings folder, and will not be overwritten.
In fact, I have just re-built the application that I knocked up to produce the code in post #7, and the settings are still in tact, so I am not sure what you are referring to.
Gary
Tyvm! I did:
and it worked perfectly on restart. Yea, don't wanna get ahead of my self. This is meh first program, just putting alot of different items on it to learn. :)vb Code:
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load For Each comboItem As String In My.Settings.MyComboItems Me.ComboBox1.Items.Add(comboItem) Next End Sub
Hey,
Glad to hear that you got it doing what you want.
Can you remember to mark your thread as resolved?
Gary
Oh sry, will do :)
I'm not quite sure what you were doing but settings are not overwritten on a build by default. That's why the Synchronize button exists on the Settings page: to delete the user config file(s) and restore User-scoped settings to their default values, which are stored in the primary config file.