来自项目设置的自动完成源 [英] Autocomplete source from project settings

查看:189
本文介绍了来自项目设置的自动完成源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想进行自动完成的字符串收集,并在运行时为搜索文本框进行编辑(向收集中添加更多文本).并在列表框中列出此集合.但是,此集合应存储在应用程序设置中,并在我重新启动应用程序时恢复.我该怎么做 ?我尝试添加System.Windows.Forms.AutoCompleteStringCollection类型的设置.

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.

我用过

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

"derslistesi"是我的应用程序设置中System.Windows.Forms.AutoCompleteStringCollection设置的名称.这没用.我无法在运行时中编辑集合成员.

"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.

当我尝试在设置页面上手动将成员添加到该集合时,出现错误,提示找不到类型为"System.String"的构造函数".

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".

推荐答案

您可以定义类型为

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 ;

这篇关于来自项目设置的自动完成源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆