Adding items in the combobox dynamically and items added by user should be permanent without using database,is possible?

11,097

Sure, just use the Items collection:

comboBox1.Items.Add(...);

And they are permanent in the way that they persist until the combo box ceases to live. If you want to retain the items through application shutdown or closing the form then you'll need to do that yourself.

You can also bind the combo box to a collection you're keeping elsewhere with the ItemsSource property. But you'll still need to take care of saving and loading the collection contents as needed.

You may want to elaborate a bit on what exactly you expect and need.

Share:
11,097
Harikrishna
Author by

Harikrishna

I am a M.C.A. student in Gujrat,India. And my e-mail address is [email protected]. I am doing my training in winform for my sixth semester of M.C.A.

Updated on June 05, 2022

Comments

  • Harikrishna
    Harikrishna almost 2 years

    Can we add the items in the combobox located on the window form dynamically ? Like there are 7 combobox on the window form and when the application is run user should be able to add the item(s) in the combobox.And items added by user should be permanent in the combobox.

  • Rich
    Rich about 14 years
    There is “window form” buried in the first sentence, so I assumed Windows Forms. Granted, not the best heuristic but might still be correct :-)
  • Harikrishna
    Harikrishna about 14 years
    Yes,There is combobox on the Window form.
  • James Westgate
    James Westgate about 14 years
    I totally misread the question Oo Need to use a database here imho.
  • Harikrishna
    Harikrishna about 14 years
    @Johanes Rossel,I have add the items run-time like combobox1.Items.Add(combobox1.Text); but item does not remain permanent when I run the application second time.
  • Rich
    Rich about 14 years
    @har: Of course it doesn't. That's why I said you'll need to do that yourself. A combobox is just a control displaying some data. Where that data comes from or where it goes is up to you as the developer. If you want it to be saved to the disk and then re-loaded after starting the application again, then you'll have to write code to do it.
  • Harikrishna
    Harikrishna about 14 years
    @James Westgate, Is not possible that without using database ? Because in my application there is no database and for only this thing I have to make connection and maintain database. So any other control that displays items and we can be able to add items in that control dynamically which should be permanent ?
  • Harikrishna
    Harikrishna about 14 years
    @Johanes Rossel, Which type of code ? Or anyother control that satisfy my this need ?
  • Dean Kuga
    Dean Kuga about 14 years
    What do you mean "Which type of code?". You need to write some code to persist the data... to flat file, to XML, to some database... that type of code... As already pointed out combobox is just a control and the data it holds is in memory only, memory is not permanent storage so you have to persist the data somewhere...
  • Rich
    Rich about 14 years
    @har: Write them to a file when the form gets closed (or immediately after adding them). Or wrap that into a custom control derived from ComboBox which does that automatically. Or use a custom collection as data source which does that automatically. There are several possibilities, depending on your exact needs and requirements.
  • James Westgate
    James Westgate about 14 years
    I would store the data in a DataSet which you create manually. Bind the combobox to the dataset. Use DataSet.WriteXml to write the contents to disk. You will also need to keep tack of the selected index. Write this into the user's documents folder so that each user gets their own version.
  • Dean Kuga
    Dean Kuga about 14 years
    Controls hold data in memory only, whenever you close and reopen your app that data is lost unless persisted to some type of storage... If you don't want to deal with the database use XML...
  • Rich
    Rich about 14 years
    @James: Not the user's documents folder, please. That's reserved for things the user puts there. Application Data is the more appropriate place. But I'm guessing explaining that will take a lot more questions by the OP ...
  • Rich
    Rich about 14 years
    @kzen: Imho, XML is pretty much overkill for a flat list of strings :-)
  • Harikrishna
    Harikrishna about 14 years
    @Johannes Rossel, Is there any custom control which does do this automatically, which is that ?
  • Rich
    Rich about 14 years
    @har: Not that I know of, no. It's usually pointless to include such very specific functionality as the very concept of “persisting data” can refer to so many ways of doing it. That's why it's left to the application developer to implement such things because there isn't a general way to solve it which would satisfy more than a few percent of users. Remember that most things in a class library should be useul to most, of not all developers. Not only a select few.
  • Harikrishna
    Harikrishna about 14 years
    @Johannes Rossel,Thanks..So I have to use xml file or database for that requirment.When we add the items in the combobox design time,then it does store in the disk ?
  • Rich
    Rich about 14 years
    @har: Well, those are saved in the Form's resources. But it doesn't have to use whatever you are implementing for persisting the values. That depends on where you do that.
  • James Westgate
    James Westgate about 14 years
    Last post here. XML - it comes for free with Dataset. Why not use the built in functionality. User document folder (sub folder) - it IS the user's data. Same as eg virtual machines etc etc. :D