问题
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