|
-
Jun 3rd, 2018, 11:33 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Add item to a combobox items collection at run time
How can I Add an item to a combobox items collection at run time, permanently not temporarily, so next time I open my interface it will be there ?. Thank you.
To add it at run time I am using this code. It adds every item I put in, but when I close and open again my app again these items are not in the combobox items collection. So Is not working.
Code:
Private Sub ComboBox1_LostFocus(sender As Object, e As EventArgs) Handles ComboBox1.LostFocus
Dim newItem As String = ComboBox1.Text
AddElement(ComboBox1, newItem)
End Sub
Code:
Sub AddElement(ByRef control As ComboBox, ByVal newItem As String)
Dim idx As Integer
If control.FindString(newItem) >= 0 Then
idx = control.FindString(newItem)
Else
idx = control.Items.Add(newItem)
End If
control.SelectedIndex = idx
End Sub
-
Jun 3rd, 2018, 03:55 PM
#2
Re: Add item to a combobox items collection at run time
The easiest way is using My.Settings, here is an example to load/save ComboBox items
* Start new project, add Button1 and ComboBox1
* Go to Project> Properties> Settings tab
* Create a new setting of type System.Collections.Specialized.StringCollection
* Paste the following code and run.
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.ComboBox1Items Is Nothing Then
' Required only for first run.
My.Settings.ComboBox1Items = New Specialized.StringCollection
End If
For Each s In My.Settings.ComboBox1Items
ComboBox1.Items.Add(s)
Next
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
My.Settings.Save()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static i As Integer
i += 1
ComboBox1.Items.Add(i)
My.Settings.ComboBox1Items.Add(i.ToString)
End Sub
End Class
Last edited by 4x2y; Jun 3rd, 2018 at 04:03 PM.
-
Jun 3rd, 2018, 04:16 PM
#3
Thread Starter
Addicted Member
Re: Add item to a combobox items collection at run time
 Originally Posted by 4x2y
The easiest way is using My.Settings, here is an example to load/save ComboBox items
* Start new project, add Button1 and ComboBox1
* Go to Project> Properties> Settings tab
* Create a new setting of type System.Collections.Specialized.StringCollection
* Paste the following code and run.
Code:
Option Strict On
Option Explicit On
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If My.Settings.ComboBox1Items Is Nothing Then
' Required only for first run.
My.Settings.ComboBox1Items = New Specialized.StringCollection
End If
For Each s In My.Settings.ComboBox1Items
ComboBox1.Items.Add(s)
Next
End Sub
Private Sub Form1_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
My.Settings.Save()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Static i As Integer
i += 1
ComboBox1.Items.Add(i)
My.Settings.ComboBox1Items.Add(i.ToString)
End Sub
End Class
At design time, in line My.Settings.ComboBox1Items a red underline and error message : ComboBox1Items is not a member of MySettings
-
Jun 3rd, 2018, 04:38 PM
#4
Re: Add item to a combobox items collection at run time
You have to create it, read again above steps (2 & 3)
-
Jun 3rd, 2018, 05:29 PM
#5
Thread Starter
Addicted Member
Re: Add item to a combobox items collection at run time
Did 2 and 3. In type I changed to System.Collections.Specialized.StringCollection. Saved but same error.
-
Jun 3rd, 2018, 05:35 PM
#6
Re: Add item to a combobox items collection at run time
I have named the setting as ComboBox1Items, if you named it different then replace ComboBox1Items with your setting name.
-
Jun 3rd, 2018, 05:44 PM
#7
Thread Starter
Addicted Member
Re: Add item to a combobox items collection at run time
 Originally Posted by 4x2y
I have named the setting as ComboBox1Items, if you named it different then replace ComboBox1Items with your setting name.
Ok, no more errors. Write USD, EUR, others. But when I open again with F5 in the combobox there are numbers like 1,2,3, ....
-
Jun 3rd, 2018, 05:55 PM
#8
Re: Add item to a combobox items collection at run time
 Originally Posted by ebellounisoft
Ok, no more errors
So, your main problem has been resolved
 Originally Posted by ebellounisoft
Write USD, EUR, others. But when I open again with F5 in the combobox there are numbers like 1,2,3, ....
This is another issue, what's type of USD, EUR?
-
Jun 3rd, 2018, 05:56 PM
#9
Thread Starter
Addicted Member
Re: Add item to a combobox items collection at run time
 Originally Posted by 4x2y
So, your main problem has been resolved
This is another issue, what's type of USD, EUR?
Words, money currency. Text.
-
Jun 3rd, 2018, 06:03 PM
#10
Re: Add item to a combobox items collection at run time
Show us your exact code, it supposed to be something like this
Code:
My.Settings.ComboBox1Items.Add(ComboBox1.Text)
-
Jun 3rd, 2018, 06:09 PM
#11
Thread Starter
Addicted Member
Re: Add item to a combobox items collection at run time
 Originally Posted by 4x2y
Show us your exact code, it supposed to be something like this
Code:
My.Settings.ComboBox1Items.Add(ComboBox1.Text)
ComboBox1 shows USD, EUR, .... but also numbers 1,2,3....
-
Jun 3rd, 2018, 06:25 PM
#12
Re: Add item to a combobox items collection at run time
You did it wrong. If you don't show us what you did, we can't tell what's wrong with it.
-
Jun 3rd, 2018, 06:30 PM
#13
Re: Add item to a combobox items collection at run time
 Originally Posted by ebellounisoft
ComboBox1 shows USD, EUR, .... but also numbers 1,2,3....
I guess these numbers are come from my sample code, i think you need to add another button to remove any entry that may added by mistake or no needed anymore, try this
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If My.Settings.ComboBox1Items.Contains(ComboBox1.Text) Then
My.Settings.ComboBox1Items.Remove(ComboBox1.Text)
ComboBox1.Items.Clear()
For Each s In My.Settings.ComboBox1Items
ComboBox1.Items.Add(s)
Next
If ComboBox1.Items.Count = 0 Then
ComboBox1.Text = String.Empty
Else
ComboBox1.SelectedIndex = 0
End If
End If
End Sub
-
Jun 3rd, 2018, 06:34 PM
#14
Thread Starter
Addicted Member
Re: Add item to a combobox items collection at run time
 Originally Posted by 4x2y
I guess these numbers are come from my sample code, i think you need to add another button to remove any entry that may added by mistake or no needed anymore, try this
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If My.Settings.ComboBox1Items.Contains(ComboBox1.Text) Then
My.Settings.ComboBox1Items.Remove(ComboBox1.Text)
ComboBox1.Items.Clear()
For Each s In My.Settings.ComboBox1Items
ComboBox1.Items.Add(s)
Next
If ComboBox1.Items.Count = 0 Then
ComboBox1.Text = String.Empty
Else
ComboBox1.SelectedIndex = 0
End If
End If
End Sub
Perfect !!!. It "Cleans" th combobox. Now I have it. Thank you.
-
Jun 3rd, 2018, 07:31 PM
#15
Re: [RESOLVED] Add item to a combobox items collection at run time
Personally, as a fan of data-binding, I would probably do it like this:
vb.net Code:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Bind the settings collection via a BindingSource. BindingSource1.DataSource = My.Settings.ComboBox1Items ComboBox1.DataSource = BindingSource1 End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Add a new item from a TextBox. BindingSource1.Add(TextBox1.Text) End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Remove the selected item. BindingSource1.RemoveCurrent() End Sub End Class
Note that there's not actually a need for code like this:
vb.net Code:
If My.Settings.ComboBox1Items Is Nothing Then ' Required only for first run. My.Settings.ComboBox1Items = New Specialized.StringCollection End If
When you add a StringCollection to your settings, note that the Value field is empty by default. That's what causes the setting property to be Nothing by default. If you edit the Value and add an item, notice that the Value field is populated with an XML snippet, which actually creates a StringCollection object. If you then edit again and remove the item, the XML remains. That way, your setting property will contain an empty StringCollection object by default and you never have to test for Nothing.
Last edited by jmcilhinney; Jun 3rd, 2018 at 07:35 PM.
-
Jun 3rd, 2018, 07:41 PM
#16
Thread Starter
Addicted Member
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by jmcilhinney
Personally, as a fan of data-binding, I would probably do it like this:
vb.net Code:
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 'Bind the settings collection via a BindingSource. BindingSource1.DataSource = My.Settings.ComboBox1Items ComboBox1.DataSource = BindingSource1 End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'Add a new item from a TextBox. BindingSource1.Add(TextBox1.Text) End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Remove the selected item. BindingSource1.RemoveCurrent() End Sub End Class
Note that there's not actually a need for code like this:
vb.net Code:
If My.Settings.ComboBox1Items Is Nothing Then ' Required only for first run. My.Settings.ComboBox1Items = New Specialized.StringCollection End If
When you add a StringCollection to your settings, note that the Value field is empty by default. That's what causes the setting property to be Nothing by default. If you edit the Value and add an item, notice that the Value field is populated with an XML snippet, which actually creates a StringCollection object. If you then edit again and remove the item, the XML remains. That way, your setting property will contain an empty StringCollection object by default and you never have to test for Nothing.
Getting a red line error message BindingSource1 not declare in BindingSource1
-
Jun 3rd, 2018, 07:50 PM
#17
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by ebellounisoft
Getting a red line error message BindingSource1 not declare in BindingSource1
So do what needs to be done to remedy that. I'm not here to do your thinking for you.
-
Jun 3rd, 2018, 07:56 PM
#18
Thread Starter
Addicted Member
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by jmcilhinney
So do what needs to be done to remedy that. I'm not here to do your thinking for you.
if you are going to give a piece of advice it must be complete.Aggressive response
-
Jun 3rd, 2018, 08:09 PM
#19
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by ebellounisoft
if you are going to give a piece of advice it must be complete.
No it doesn't. It can be as much or as little as I care to provide. I'm not here to spoon-feed and wipe noses. If people post here then they obviously think that they have the potential to develop software so I credit them with having that potential. I'm here to help with the tough problems that they are unable to work out on their own. Simple problems like this should not require my help. If they really do then it is my opinion that the person doesn't really have the potential required.
 Originally Posted by ebellounisoft
Aggressive response
When my responses are perceived as aggressive, it's usually because I'm responding to a lazy request. If you don't want me to be what you perceive as aggressive then don't be what I perceive as lazy. For start, do you know what a BindingSource is? If not, what effort have you made to find out? See, while you think I have some responsibility to provide you with every possible detail, I know that VS has a Help menu and there are numerous search engines that allow you to find information for yourself with very little effort. If you're not prepared to put in that very little effort to help yourself, why should I put more effort in for you? The fact is that you are free to put in as little effort as you like but so am I. There is no "must". I am prepared to help those who help themselves.
-
Jun 3rd, 2018, 08:12 PM
#20
Thread Starter
Addicted Member
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by jmcilhinney
No it doesn't. It can be as much or as little as I care to provide. I'm not here to spoon-feed and wipe noses. If people post here then they obviously think that they have the potential to develop software so I credit them with having that potential. I'm here to help with the tough problems that they are unable to work out on their own. Simple problems like this should not require my help. If they really do then it is my opinion that the person doesn't really have the potential required.
When my responses are perceived as aggressive, it's usually because I'm responding to a lazy request. If you don't want me to be what you perceive as aggressive then don't be what I perceive as lazy. For start, do you know what a BindingSource is? If not, what effort have you made to find out? See, while you think I have some responsibility to provide you with every possible detail, I know that VS has a Help menu and there are numerous search engines that allow you to find information for yourself with very little effort. If you're not prepared to put in that very little effort to help yourself, why should I put more effort in for you? The fact is that you are free to put in as little effort as you like but so am I. There is no "must". I am prepared to help those who help themselves.
I had already closed the post and you were the one who proposed the supposed solution. I never asked for it.
-
Jun 3rd, 2018, 08:18 PM
#21
Thread Starter
Addicted Member
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by jmcilhinney
No it doesn't. It can be as much or as little as I care to provide. I'm not here to spoon-feed and wipe noses. If people post here then they obviously think that they have the potential to develop software so I credit them with having that potential. I'm here to help with the tough problems that they are unable to work out on their own. Simple problems like this should not require my help. If they really do then it is my opinion that the person doesn't really have the potential required.
When my responses are perceived as aggressive, it's usually because I'm responding to a lazy request. If you don't want me to be what you perceive as aggressive then don't be what I perceive as lazy. For start, do you know what a BindingSource is? If not, what effort have you made to find out? See, while you think I have some responsibility to provide you with every possible detail, I know that VS has a Help menu and there are numerous search engines that allow you to find information for yourself with very little effort. If you're not prepared to put in that very little effort to help yourself, why should I put more effort in for you? The fact is that you are free to put in as little effort as you like but so am I. There is no "must". I am prepared to help those who help themselves.
We are not all as advanced as you are. Answer the most advanced.
-
Jun 3rd, 2018, 08:20 PM
#22
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by ebellounisoft
I had already closed the post and you were the one who proposed the supposed solution. I never asked for it.
Wow, bold and red. If only you went to as much effort to find information as you do to format your posts.
You had indeed marked the thread Resolved and you did not solicit further information. I guess I must be a terrible person for thinking that I might have some additional information that might help you further. I provided that information. It's your choice to either use it or ignore it. You chose the former and then you DID ask for something, but it was something that you could/should have been able to work out for yourself. I come here to help people who want to help themselves, not to do work for people who aren't prepared to do it for themselves. I've given you as much as I think you need if you are the former. If you're the latter then I have no further interest. My contribution to this conversation is at an end unless I see a genuine effort on your part to solve what is a very simple problem.
-
Jun 3rd, 2018, 08:24 PM
#23
Thread Starter
Addicted Member
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by jmcilhinney
Wow, bold and red. If only you went to as much effort to find information as you do to format your posts.
You had indeed marked the thread Resolved and you did not solicit further information. I guess I must be a terrible person for thinking that I might have some additional information that might help you further. I provided that information. It's your choice to either use it or ignore it. You chose the former and then you DID ask for something, but it was something that you could/should have been able to work out for yourself. I come here to help people who want to help themselves, not to do work for people who aren't prepared to do it for themselves. I've given you as much as I think you need if you are the former. If you're the latter then I have no further interest. My contribution to this conversation is at an end unless I see a genuine effort on your part to solve what is a very simple problem.
You have time to give lectures about procedures but not for a simple help. Please do not write more. You already told me that you are very intelligent and a genius for questions like mine Please do not write more.
Last edited by ebellounisoft; Jun 3rd, 2018 at 08:28 PM.
-
Jun 3rd, 2018, 08:48 PM
#24
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by ebellounisoft
You have time to give lectures about procedures but not for a simple help.
It's not a matter of time but rather inclination. Believe it or not, I want you to be the best developer you can be and it is my belief that that will never happen if you're not prepared to put the effort in to solve simple issues like this for yourself. If people like me encourage you to rely on us for things that take little effort, you'll then put in very little effort and not learn how to solve problems and thus not learn how to be a decent developer.
 Originally Posted by ebellounisoft
You already told me that you are very intelligent and a genius for questions like mine
Except I told you no such thing. People like to pretend things like this so that they can tell themselves that they are justified in their outrage but it's just not true. I've never made any claims about my level of intelligence and, what's more, I'm crediting you with just as much intelligence as I have to be able to solve this simple problem. If I'm wrong then we have both been wasting our time from the outset. You can keep telling yourself that I am being unreasonable and refusing to apply your mind to the problem to "prove" your point or you can make the effort to solve an easy problem. My life is going to go on the same regardless. If you won't help yourself then it's you who will suffer.
Despite what you might want to think, I post here to help people. It's my position that pushing people to use their own intelligence to solve their own problems wherever possible is in their best interests. I didn't expect you to be able to work out for yourself the information that I provided in post #15 and that's why I provided it. I do expect you to be able to do what's required to clear this last error message. If I tell you what's required then I'm encouraging you to rely on others to do your thinking for you and that will not help to make you a better developer. I'm doing what I think is best for you as a developer. You are free to disagree and you are free to ignore everything I say if you want but you have said and done nothing to convince me that it would be better for me to solve a problem for you that you could easily solve for yourself.
-
Jun 3rd, 2018, 09:04 PM
#25
Thread Starter
Addicted Member
Re: [RESOLVED] Add item to a combobox items collection at run time
 Originally Posted by jmcilhinney
It's not a matter of time but rather inclination. Believe it or not, I want you to be the best developer you can be and it is my belief that that will never happen if you're not prepared to put the effort in to solve simple issues like this for yourself. If people like me encourage you to rely on us for things that take little effort, you'll then put in very little effort and not learn how to solve problems and thus not learn how to be a decent developer.
Except I told you no such thing. People like to pretend things like this so that they can tell themselves that they are justified in their outrage but it's just not true. I've never made any claims about my level of intelligence and, what's more, I'm crediting you with just as much intelligence as I have to be able to solve this simple problem. If I'm wrong then we have both been wasting our time from the outset. You can keep telling yourself that I am being unreasonable and refusing to apply your mind to the problem to "prove" your point or you can make the effort to solve an easy problem. My life is going to go on the same regardless. If you won't help yourself then it's you who will suffer.
Despite what you might want to think, I post here to help people. It's my position that pushing people to use their own intelligence to solve their own problems wherever possible is in their best interests. I didn't expect you to be able to work out for yourself the information that I provided in post #15 and that's why I provided it. I do expect you to be able to do what's required to clear this last error message. If I tell you what's required then I'm encouraging you to rely on others to do your thinking for you and that will not help to make you a better developer. I'm doing what I think is best for you as a developer. You are free to disagree and you are free to ignore everything I say if you want but you have said and done nothing to convince me that it would be better for me to solve a problem for you that you could easily solve for yourself.
You should be a Minister and not a software consultant. The time you have spent philosophizing, would have already written a code proposal. Again I do not care what you think and do not write anymore.
-
Jun 3rd, 2018, 09:09 PM
#26
Thread Starter
Addicted Member
Re: [RESOLVED] Add item to a combobox items collection at run time
Please ask jmcilhinney to stop harassing me with his extensive comments
-
Jun 4th, 2018, 04:25 AM
#27
Re: [RESOLVED] Add item to a combobox items collection at run time
Honestly, neither of you is covering yourself in glory.
@ebellounisoft, try to understand that, in trying to help you, people are doing you a favour and that means they got to do so on their own terms. They get to provide as much or as little help as they choose and it behoves us to be grateful for any help we receive.
In this case it would have paid you to do a bit of research into databinding if you were going to adopt JMcIlhinney's suggestion. It certainly would have paid you to do some research on the error itself once you started getting it. Just grabbing peoples code off the internet and using it without fully understanding it isn't a great idea. At best it means you'll fail to learn new things. At worst it's dangerous because you don't know what you're program now does. It could be sending all your clients financial details to a third party. You don't owe it to Jm, me or anyone else to take this advice, you owe it to yourself. It means a little more work now but in the long run it will make you a better programmer.
@JMcIlhinney, come on. You're a senior member round here and worthy of respect but that doesn't give you the right to berate other members. I appreciate your intention was to help and you're offering good advice but, when that advice isn't taken, just move on. Lectures about forum etiquette only serve to alienate other members and it won't just be the member on the receiving end either. It will also be every potential new member who comes across this thread and is discouraged from posting their own lest they receive the same treatment. That's not what we want.
@Both of you, I don't really see a material difference between bold red text and extensive lecture. Both come across as aggressive and make you look bad.
You are both valuable members of this forum. Jm for his experience and ebellounisoft for his potential. We want you both on here asking and answering questions in as constructive a way as possible and I hope you'll both continue to contribute.
Since this thread has already been resolved and doesn't appear to be going anywhere useful I'm going to close it.
The best argument against democracy is a five minute conversation with the average voter - Winston Churchill
Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd
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
|