Results 1 to 6 of 6

Thread: [RESOLVED] How to add items to a checked listbox

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    17

    Resolved [RESOLVED] How to add items to a checked listbox

    I was wondering if there was any tut on how to aan item to a checked listbox thats already populated?

    I thought of making a button and a text box .and when u push the button it add whats ever in the text box to the checklist box . but cant find any tut or example any help is appreciated.

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

    Re: How to add items to a checked listbox

    The CheckedListBox has an Items property. That collection has an Add method. That's it. You don't need a tutorial. You just need to read the documentation.

    http://msdn.microsoft.com/en-us/libr...edlistbox.aspx

    That Help topic has a code example that shows the aforementioned method being called. Moreover, it even shows an item being added form a TextBox on the Click of a Button, i.e. exactly what you're suggesting. ALWAYS read the documentation first.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    17

    Re: How to add items to a checked listbox

    sorry id dont think i worded it right i want to the end user to be able to add things to the check box list .i know how to add them myself...but it just somemethod for the end user of my db to add stuff to the check box without me having to update the program...


    hope that explains what im loking for..

  4. #4
    Lively Member
    Join Date
    Aug 2010
    Location
    Look to the right... yep, there i am.
    Posts
    111

    Re: How to add items to a checked listbox

    Add a TextBox and a Button

    Under the button's code, add this:
    Code:
    CheckedListBox1.Items.Add(TextBox1.Text)
    Basic. if you get errors work it out... and if you can't figure it out sendz me a pm.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to add items to a checked listbox

    There is only one way to add items to a CheckedListBox and that's in code. Even when you add items in the designer, that simply generates the code to add them for you. So, if you want the user to be able to add items then you do it as already explained.

    If you want to save data between sessions then that is a different thing altogether, and doesn't really relate to the CheckedListBox specifically. The principle would be the same no matter what the data was and how it was being displayed. Basically, you need to persist the data somewhere before closing and then load it again at startup. That somewhere could be a database, a text file, an XML file, applications settings or various other options.

    My first suggestion would be to use application settings. To do that, you can open the Settings page of the project properties and add a StringCollection. You would then add your default items to that. At startup, load the items from that StringCollection into your control. Add new items to the control during the session and, before you shut down, refresh the StringCollection with the contents of the control.

    One issue there is that the checked state of the items won't be saved. To handle that you could combine the text and checked state of an item into a single string to store in the setting. You can then parse the data on load to get the two separate values.

    Using My.Settings will also use a separate list for each Windows user. That might not matter if only one person uses the machine. It also might be exactly what you want, or it might not be acceptable. In that final case, you'd have to use some other method to persist the data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Dec 2010
    Posts
    17

    Re: How to add items to a checked listbox

    thanks for the help guyz

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