Autocomplete source from project settings

强颜欢笑 提交于 2019-12-08 03:22:12

问题


I want to make an autocomplete string collection and edit it on runtime (add more text to collection) for a search textbox. And list this collection in a listbox. But this collection should be stored in application settings and be restored when i restart the application. How can i do it ? I tried adding a System.Windows.Forms.AutoCompleteStringCollection type of setting.

I used

string newsuggestion = textBox1.Text;
Settings.Default.derslistesi.Add(newsuggestion);

"derslistesi" is the name of the System.Windows.Forms.AutoCompleteStringCollection setting in my application settings. This didn't work. I couldn't edit collection members in runtime.

When i tried to manually add a member to that collection on settings page, i got an error that says "Constructor on type "System.String" not found".


回答1:


You can define a setting property of type System.Collections.Specialized.StringCollection and name it for example MyProperty. You can also add some values to it using designer.

To add values to the collection at run-time:

Properties.Settings.Default.MyProperty.Add("Some Value");
Properties.Settings.Default.Save();

To set values as auto-complete source for your text box:

var source = new AutoCompleteStringCollection();
source.AddRange(Properties.Settings.Default.MyProperty.Cast<string>().ToArray());
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = source ;


来源:https://stackoverflow.com/questions/40370923/autocomplete-source-from-project-settings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!