|
-
Jul 7th, 2010, 06:28 AM
#1
Thread Starter
Lively Member
[Resolved] ComboBox Add/Save
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.
Last edited by Rivkah; Jul 7th, 2010 at 03:25 PM.
-
Jul 7th, 2010, 08:38 AM
#2
Re: ComboBox Add/Save
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
-
Jul 7th, 2010, 08:38 AM
#3
Re: ComboBox Add/Save
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.
-
Jul 7th, 2010, 08:40 AM
#4
Re: ComboBox Add/Save
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
-
Jul 7th, 2010, 10:00 AM
#5
Thread Starter
Lively Member
Re: ComboBox Add/Save
well from what i understood, I could do this exactly like I save a textbox. so I made a button with the code:
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
but I get the error:
Code:
Value of type 'System.Windows.Forms.Combobox.ObjectCollection' cannot be converted to 'System.Collections.Specialized.String.Collection'.
Oh, and the setting i did was:
vb Code:
Name: ComboBox1 Type: System.Collections.Specialized.StringCollection Scope: User Value:
Incase you alrdy didnt know: I am not very good with VB ><
-
Jul 7th, 2010, 01:34 PM
#6
Thread Starter
Lively Member
-
Jul 7th, 2010, 02:51 PM
#7
Re: ComboBox Add/Save
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:
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
And this when you load them back up again:
Code:
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
Gary
-
Jul 7th, 2010, 03:02 PM
#8
Re: ComboBox Add/Save
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.
-
Jul 7th, 2010, 03:03 PM
#9
Thread Starter
Lively Member
Re: ComboBox Add/Save
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
Last edited by Rivkah; Jul 7th, 2010 at 03:08 PM.
-
Jul 7th, 2010, 03:11 PM
#10
Re: ComboBox Add/Save
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
-
Jul 7th, 2010, 03:12 PM
#11
Re: ComboBox Add/Save
 Originally Posted by Radjesh Klauke
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.
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
-
Jul 7th, 2010, 03:16 PM
#12
Thread Starter
Lively Member
Re: ComboBox Add/Save
Tyvm! I did:
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
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.
-
Jul 7th, 2010, 03:21 PM
#13
Re: ComboBox Add/Save
Hey,
Glad to hear that you got it doing what you want.
Can you remember to mark your thread as resolved?
Gary
-
Jul 7th, 2010, 03:25 PM
#14
Thread Starter
Lively Member
Re: ComboBox Add/Save
Oh sry, will do
-
Jul 7th, 2010, 06:23 PM
#15
Re: ComboBox Add/Save
 Originally Posted by Radjesh Klauke
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.
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.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|