Results 1 to 19 of 19

Thread: [RESOLVED] adding data to a combobox.list.controlset

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Resolved [RESOLVED] adding data to a combobox.list.controlset

    Hi, this is probably really dumb but i have spent the last 2 days scouring the forums to see if i can find an answer, i am not a coder or even working in an IT field but enjoy trying to do things with code.
    My problem is i have created a form and it has a combobox that is filled with items that were hard coded at time of creation (it will never be more than 30 items and was done in combobox.databindings.items.collection) so no need for db or anything.
    However i want the ability to add extra items to this combobox.items.collection from a textbox it needed.
    I have already got this to work but as soon as i close the program it does not retain the newly added item.
    I started to look into bindingsource but that just totally confused me and i am not even sure that is the fix
    Again i have very limited knowledge but am hoping someone can point me in the right direction
    Thanks in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: adding data to a combobox.list.controlset

    Not a great start. You have posted in the CodeBank forum, which is for sharing working code snippets, rather than asking questions. I have asked the mods to move this thread.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: adding data to a combobox.list.controlset

    As for the issue, what EXACTLY is the ComboBox bound to? I'll test to confirm but I think that you should be able to add a StringCollection to your application settings, bind to that and then have changes persisted to the user config file between sessions.

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    Quote Originally Posted by jmcilhinney View Post
    As for the issue, what EXACTLY is the ComboBox bound to? I'll test to confirm but I think that you should be able to add a StringCollection to your application settings, bind to that and then have changes persisted to the user config file between sessions.

    Thanks for getting back, i think that that is my problem that it is not bound to anything i am aware of, and was not thinking it needed to be.

    When i look at databindings and try and add or select an option it juts says "none"

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    Thanks yeah i thought that after i asked the question

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    Quote Originally Posted by Graeme_J View Post
    Thanks for getting back, i think that that is my problem that it is not bound to anything i am aware of, and was not thinking it needed to be.

    When i look at databindings and try and add or select an option it juts says "none"
    I found your article copied below from a couple of years ago yesterday but i just got totally lost with how / databinding is setup
    Sorry for my ignorance as this seemed to be what i needed just reading it but had no clue where to look

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: adding data to a combobox.list.controlset

    Can I just confirm that this is definitely a VB.NET Windows Forms application? Also, assuming that it is, is it targeting .NET 5.0, some version of .NET Core or some version of .NET Framework? You can see that in the project properties and, if it's one of the last two, which version?

  8. #8

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    Yes .net 5.0 and windows application, using visual studio 2019 and selected windows forms application at startup when i cretaed the project

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: adding data to a combobox.list.controlset

    Here's the steps you should follow to use the code you posted in post #6:

    1. Create a new project using the Windows Forms App (.NET Framework) template.
    2. From the Toolbox, add to your form a ComboBox, a TextBox, two Buttons and a BindingSource.
    3. Open the project properties. There are a number of ways to do that and I usually double-click the My Project node in the Solution Explorer. Go to the Settings page.
    4. Add a setting named ComboBox1Items of type System.Collections.Specialized.StringCollection with User scope.
    5. Click in the Value field and click the (...) button, then add a number of default items and click OK. Notice the XML snippet added, which is what gets stored in the config file.
    6. In the form, create a handler for the Load event and bind the items to the control:
    vb.net Code:
    1. Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    2.     BindingSource1.DataSource = My.Settings.ComboBox1Items
    3.     ComboBox1.DataSource = BindingSource1
    4. End Sub
    Run the project and notice that the ComboBox contains the items that you added to the collection in your settings.

    7. Create a handler for the Click event of Button1 and add code to add a new item to the list:
    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.     Dim newItem = TextBox1.Text
    3.  
    4.     If newItem.Trim() <> String.Empty Then
    5.         BindingSource1.Add(newItem)
    6.     End If
    7.  
    8.     TextBox1.Clear()
    9.     TextBox1.Select()
    10. End Sub
    Run the project, add some text to the TextBox and click Button1. Notice that a new item is added to the ComboBox. Close the app and run the project again. Notice that the added item(s) is still in the ComboBox.

    8. Create a handler for the Click event of Button2 and add code to remove the selected item from the list:
    vb.net Code:
    1. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    2.     BindingSource1.RemoveCurrent()
    3. End Sub
    Run the project, select an item in the ComboBox and click Button2. Notice that the selected item is removed from the ComboBox. Close the app and run the project again. Notice that the removed item(s) is still absent from the ComboBox.

    What's happening here is that settings you add in the project properties are stored in the config file. Those with Application scope are stored in the application config file and are read-only via My.Settings in code. Those with User scope have their default value stored in the application config file and their current value stored in a user config file under the personal folder of each Windows user who runs the app. If you open the App.config file from the Solution Explorer then you can see the contents of the application config file.

    At run time, the data is loaded from the appropriate config file into the My.Settings object. Because our code binds My.Settings.ComboBox1Items to BindingSource1, the changes we make via BindingSource1 directly affect that collection. By default, changes made to My.Settings are automatically saved back to the user config file when the app exits. You can change that behaviour on the Application page of the project properties and, whether changes are saved automatically or not, you can save them manually at any time by calling My.Settings.Save. Some people like to do that because it means that changes won't be lost if your app crashes or the system shuts down abnormally while your app is running.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: adding data to a combobox.list.controlset

    Quote Originally Posted by Graeme_J View Post
    Yes .net 5.0 and windows application, using visual studio 2019 and selected windows forms application at startup when i cretaed the project
    I think that what I have described above will work for .NET 5.0 (which is based on .NET Core rather than .NET Framework) but I would suggest that you target .NET Framework rather than .NET 5.0 for the time being, unless you have a specific reason for doing otherwise. Windows Forms can't be cross-platform yet anyway and there are still a few features missing from WinForms.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: adding data to a combobox.list.controlset

    I just tested and the same steps will lead to the same result in a .NET 5.0 project with one caveat. Adding the setting will generate the App.config file, just as before. You will need to open that file and delete the entire <system.diagnostics> node, as that causes problems for .NET Core apps.

  12. #12

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    thanks will read thru all of your reply and have a go tonight

  13. #13

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    just quickly , i selected a windows form app that allows me to select a version of .net framework rather than the .net 5 or other core so will see how i go
    cheers

  14. #14

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    Thanks so much just tried it and it works as you said, The screen looks different to my original which explains why when i was trying to follow your posts for the last few days and in the .net5 environment i could not figure it out
    again thanks for your help, i will now attempt to re do all the code i hade in my old version to exist in this environment and see where it gets me
    Regards

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: adding data to a combobox.list.controlset

    There are advantages to .NET Core and .NET 5.0 but for a beginner creating WinForms apps, they don't really apply. It's better to stick with .NET Framework for now, if only because almost everything you read online will apply to .NET Framework projects. Once .NET 6.0 arrives, the final few missing pieces may be added and there won't be any issues using it instead of .NET Framework 4.8. There will always be some subtle differences though, so you should always bear that in mind. Support for .NET Framework 4.8 will be around for a long time yet so, especially for learning purposes, there's little to be lost right now by using it. That's especially true for WinForms development, which is not changing the way ASP.NET is.

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: adding data to a combobox.list.controlset

    Thread moved from the 'CodeBank VB.Net' forum (which is for you to post working code examples, not questions) to the 'VB.Net' forum

  17. #17

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    I was having a play around to see if i was understanding what was created so i presumed if i wanted to duplicate the instance and have another combobox doing the same with different data i just needed to replicate the steps you showed me and change the naming convention.
    But i ended up with an error telling me that the second bindingsource2 (replica of the original) is "not declared may be inaccessable due to its protection"

    I thought we declared this in the properties settings when we created the My.Settings Name and Type ? as i see no other declaration for the original BindingSource1

  18. #18
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: adding data to a combobox.list.controlset

    Quote Originally Posted by Graeme_J View Post
    I was having a play around to see if i was understanding what was created so i presumed if i wanted to duplicate the instance and have another combobox doing the same with different data i just needed to replicate the steps you showed me and change the naming convention.
    But i ended up with an error telling me that the second bindingsource2 (replica of the original) is "not declared may be inaccessable due to its protection"

    I thought we declared this in the properties settings when we created the My.Settings Name and Type ? as i see no other declaration for the original BindingSource1
    The BindingSource is added to the form in the designer, just like the controls (step 2 in post #9). It is a component, which is all that's required for the designer. Controls are specialised components. If you didn't add the ComboBox to the form then you wouldn't expect to be able to use it in code, right? Same goes for the BindingSource.

  19. #19

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    10

    Re: adding data to a combobox.list.controlset

    thanks that was the step i missed i appreciate your time
    thanks you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width