views:

375

answers:

2

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.

+1  A: 

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.

Joey
@Johanes Rossel,It can not be permanent in this way..
Harikrishna
Care to elaborate?
Joey
@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.
Harikrishna
@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.
Joey
@Johanes Rossel, Which type of code ? Or anyother control that satisfy my this need ?
Harikrishna
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...
kzen
@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.*
Joey
@Johannes Rossel, Is there any custom control which does do this automatically, which is that ?
Harikrishna
@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.
Joey
@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 ?
Harikrishna
@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.
Joey
A: 

If you are using ASP.NET by default the items will persist in the viewstate and the selected item will remain between postbacks. You will only need to load the combobox once.

James Westgate
There is “window form” buried in the first sentence, so I assumed Windows Forms. Granted, not the best heuristic but might still be correct :-)
Joey
Yes,There is combobox on the Window form.
Harikrishna
I totally misread the question OoNeed to use a database here imho.
James Westgate
@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
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.
James Westgate
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...
kzen
@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 ...
Joey
@kzen: Imho, XML is pretty much overkill for a flat list of strings :-)
Joey
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
James Westgate