windows 动态添加组合框中的项目和用户添加的项目应该是永久的而不使用数据库,这可能吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2570668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding items in the combobox dynamically and items added by user should be permanent without using database,is possible?
提问by Harikrishna
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 permanentin the combobox.
我们可以动态添加位于窗体上的组合框中的项目吗?就像窗口窗体上有 7 个组合框一样,当应用程序运行时,用户应该能够在组合框中添加项目。用户添加的项目在组合框中应该是永久的。
采纳答案by Joey
Sure, just use the Items
collection:
当然,只需使用Items
集合:
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.
您还可以将组合框绑定到您在ItemsSource
属性的其他地方保留的集合。但是您仍然需要根据需要保存和加载集合内容。
You may want to elaborate a bit on what exactly you expect and need.
您可能想要详细说明您的期望和需要。
回答by James Westgate
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.
如果默认情况下使用 ASP.NET,项目将保留在视图状态中,所选项目将保留在回发之间。您只需要加载一次组合框。