C# 谷歌提示文本框(自动完成)
开发一个文本框的最佳方法是什么?该文本框可以记住最后输入的x个条目数。这是一个用C#编写的独立应用程序。这实际上相当简单,尤其是在显示“自动完成”部分方面。在记住最后X个条目的情况下,你将不得不决定一个特定的事件(或事件),你认为这是一个完成的条目,并把这个条目写进一个列表…准确地说,这是一个AutoCompleteTestringCollection TextBox类具有以下3个您需要的属性:C# 谷歌提示文本框(自动完成),c#,winforms,autocomplete,C#,Winforms,Autocomplete,开发一个文本框的最佳方法是什么?该文本框可以记住最后输入的x个条目数。这是一个用C#编写的独立应用程序。这实际上相当简单,尤其是在显示“自动完成”部分方面。在记住最后X个条目的情况下,你将不得不决定一个特定的事件(或事件),你认为这是一个完成的条目,并把这个条目写进一个列表…准确地说,这是一个AutoCompleteTestringCollection TextBox类具有以下3个您需要的属性: 自动完成自定义源 自动完成模式 自动完成源 将AutoCompleteMode设置为Sugges
- 自动完成自定义源
- 自动完成模式
- 自动完成源
名称空间自动完成
{
公共部分类Main:表单
{
//因此,您不必每次都寻址“txtMain.AutoCompleteCustomSource”
自动完成测试环收集;
公用干管()
{
初始化组件();
//设置为使用自定义源
txtMain.AutoCompleteSource=AutoCompleteSource.CustomSource;
//设置为显示下拉列表*和*将当前建议附加到结尾
txtMain.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
//初始化字符串集合。
acsc=新的AutoCompleteTestringCollection();
//将txtMain的自动完成源设置为acsc
txtMain.AutoCompleteCustomSource=acsc;
}
私有void txtMain_KeyDown(对象发送方,KeyEventArgs e)
{
如果(e.KeyCode==Keys.Enter)
{
//仅保留10个自动完成字符串
如果(acsc计数<10)
{
//添加到集合
acsc.Add(txtMain.Text);
}
其他的
{
//删除最旧的
acsc.RemoveAt(0);
//添加到集合
acsc.Add(txtMain.Text);
}
}
}
私有void Main_FormClosed(对象发送方,FormClosedEventArgs e)
{
//打开流以自动完成保存文件
StreamWriter sw=新的StreamWriter(“AutoComplete.acs”);
//将AutoCompleteTestringCollection写入流
foreach(acsc中的字符串s)
西南书写线(s);
//刷新到文件
sw.Flush();
//清理
sw.Close();
sw.Dispose();
}
私有void主加载(对象发送方、事件参数)
{
//打开流以自动完成保存文件
StreamReader sr=新的StreamReader(“AutoComplete.acs”);
//初读
字符串行=sr.ReadLine();
//循环直到结束
while(行!=null)
{
//添加到AutoCompleteTestringCollection
acsc.Add(行);
//再读一遍
line=sr.ReadLine();
}
//清理
高级关闭();
高级处置();
}
}
}
这段代码将完全按照原样工作,您只需要使用名为txtMain的文本框创建GUI,并将KeyDown、Closed和Load事件连接到文本框和Main表单
还要注意的是,对于这个示例,为了使其简单化,我只是选择检测按下Enter键作为触发器,以将字符串保存到集合中。根据您的需要,可能会有更多/不同的活动更好
此外,用于填充集合的模型不是很“智能”,它只是在集合达到10的限制时删除最旧的字符串。这可能并不理想,但对于示例来说是有效的。你可能会想要某种评级系统(特别是如果你真的希望它是谷歌的话)
最后请注意,这些建议实际上将按照它们在集合中的顺序显示。如果出于某种原因,您希望它们以不同的方式显示,只需按您喜欢的方式对列表进行排序即可
希望有帮助 我将完成列表存储在注册表中 我使用的代码如下。它可重复使用,分为三个步骤:
[assembly:AssemblyProduct(“…”)
和[assembly:AssemblyCompany(“…”)
。(这些属性通常在VisualStudio中创建的项目中自动设置,因此我不将其视为一个步骤。)
玛瑙
namespace AutoComplete
{
public partial class Main : Form
{
//so you don't have to address "txtMain.AutoCompleteCustomSource" every time
AutoCompleteStringCollection acsc;
public Main()
{
InitializeComponent();
//Set to use a Custom source
txtMain.AutoCompleteSource = AutoCompleteSource.CustomSource;
//Set to show drop down *and* append current suggestion to end
txtMain.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
//Init string collection.
acsc = new AutoCompleteStringCollection();
//Set txtMain's AutoComplete Source to acsc
txtMain.AutoCompleteCustomSource = acsc;
}
private void txtMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Only keep 10 AutoComplete strings
if (acsc.Count < 10)
{
//Add to collection
acsc.Add(txtMain.Text);
}
else
{
//remove oldest
acsc.RemoveAt(0);
//Add to collection
acsc.Add(txtMain.Text);
}
}
}
private void Main_FormClosed(object sender, FormClosedEventArgs e)
{
//open stream to AutoComplete save file
StreamWriter sw = new StreamWriter("AutoComplete.acs");
//Write AutoCompleteStringCollection to stream
foreach (string s in acsc)
sw.WriteLine(s);
//Flush to file
sw.Flush();
//Clean up
sw.Close();
sw.Dispose();
}
private void Main_Load(object sender, EventArgs e)
{
//open stream to AutoComplete save file
StreamReader sr = new StreamReader("AutoComplete.acs");
//initial read
string line = sr.ReadLine();
//loop until end
while (line != null)
{
//add to AutoCompleteStringCollection
acsc.Add(line);
//read again
line = sr.ReadLine();
}
//Clean up
sr.Close();
sr.Dispose();
}
}
}
namespace Ionic.ExampleCode
{
public partial class NameOfYourForm
{
private void SaveFormToRegistry()
{
if (AppCuKey != null)
{
// the completion list
var converted = _completions.ToList().ConvertAll(x => x.XmlEscapeIexcl());
string completionString = String.Join("¡", converted.ToArray());
AppCuKey.SetValue(_rvn_Completions, completionString);
}
}
private void FillFormFromRegistry()
{
if (!stateLoaded)
{
if (AppCuKey != null)
{
// get the MRU list of .... whatever
_completions = new System.Windows.Forms.AutoCompleteStringCollection();
string c = (string)AppCuKey.GetValue(_rvn_Completions, "");
if (!String.IsNullOrEmpty(c))
{
string[] items = c.Split('¡');
if (items != null && items.Length > 0)
{
//_completions.AddRange(items);
foreach (string item in items)
_completions.Add(item.XmlUnescapeIexcl());
}
}
// Can also store/retrieve items in the registry for
// - textbox contents
// - checkbox state
// - splitter state
// - and so on
//
stateLoaded = true;
}
}
}
private Microsoft.Win32.RegistryKey AppCuKey
{
get
{
if (_appCuKey == null)
{
_appCuKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(AppRegistryPath, true);
if (_appCuKey == null)
_appCuKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(AppRegistryPath);
}
return _appCuKey;
}
set { _appCuKey = null; }
}
private string _appRegistryPath;
private string AppRegistryPath
{
get
{
if (_appRegistryPath == null)
{
// Use a registry path that depends on the assembly attributes,
// that are presumed to be elsewhere. Example:
//
// [assembly: AssemblyCompany("Dino Chiesa")]
// [assembly: AssemblyProduct("XPathVisualizer")]
var a = System.Reflection.Assembly.GetExecutingAssembly();
object[] attr = a.GetCustomAttributes(typeof(System.Reflection.AssemblyProductAttribute), true);
var p = attr[0] as System.Reflection.AssemblyProductAttribute;
attr = a.GetCustomAttributes(typeof(System.Reflection.AssemblyCompanyAttribute), true);
var c = attr[0] as System.Reflection.AssemblyCompanyAttribute;
_appRegistryPath = String.Format("Software\\{0}\\{1}",
p.Product, c.Company);
}
return _appRegistryPath;
}
}
private Microsoft.Win32.RegistryKey _appCuKey;
private string _rvn_Completions = "Completions";
private readonly int _MaxMruListSize = 14;
private System.Windows.Forms.AutoCompleteStringCollection _completions;
private bool stateLoaded;
}
public static class Extensions
{
public static string XmlEscapeIexcl(this String s)
{
while (s.Contains("¡"))
{
s = s.Replace("¡", "¡");
}
return s;
}
public static string XmlUnescapeIexcl(this String s)
{
while (s.Contains("¡"))
{
s = s.Replace("¡", "¡");
}
return s;
}
public static List<String> ToList(this System.Windows.Forms.AutoCompleteStringCollection coll)
{
var list = new List<String>();
foreach (string item in coll)
{
list.Add(item);
}
return list;
}
}
}