文本框自动完成无法正常工作 [英] TextBox AutoComplete Not working properly

查看:14
本文介绍了文本框自动完成无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用数组形式的自定义数据源实现文本框自动完成功能,该数组显示有关单字符输入的建议.但是当我运行程序时,自动完成仅在第二个字符输入后开始.当我调试时,我看到 AutoCompleteNameCollection 中有数据,但直到第二个字符输入才显示在建议中.

I am trying to implement a textbox autocomplete with a custom datasource in the form of an array which shows suggestions on single character input. But when i run the program the autocomplete only starts after the second character input. When i debugged i saw that datas are there in the AutoCompleteNameCollection but it is not showing in the suggestion until the second character input.

这段代码是我在 textchange 事件中写的.

This code i have written in the textchange event.

arr = LoadName(empid_txt.Text.Trim());  //arr is string array                          
namesCollection.AddRange(arr);
this.empid_txt.AutoCompleteMode = AutoCompleteMode.Suggest;
this.empid_txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
this.empid_txt.AutoCompleteCustomSource = namesCollection;

在 LoadEvent 中,我使用空数组初始化了 AutoCompleteNameCollection.

In the LoadEvent I have initialized the AutoCompleteNameCollection with an empty array.

namesCollection.AddRange(arr);// here arr is empty
empid_txt.AutoCompleteMode = AutoCompleteMode.Suggest;
empid_txt.AutoCompleteSource = AutoCompleteSource.CustomSource;
empid_txt.AutoCompleteCustomSource = namesCollection;

但在输入第二个字符之前它不起作用.但是,如果我使用空字符串而不是数组来初始化 AutoCompleteNameCollection,例如: namesCollection.Add("");它确实适用于单个字符输入,但有时它会给出 AccessViolationException: Attempted to read or write protected memory.那么有什么办法可以解决这个问题吗?

But it does't work until the second character input. However if instead of an array i initialize the AutoCompleteNameCollection with an empty string like : namesCollection.Add(""); it does work for the single character input but some times it gives AccessViolationException: Attempted to read or write protected memory. So is there any way i can solve this problem?

推荐答案

在加载时,您可以使用数据的子集填充 TextBox(甚至可以缓存以供将来/共享使用).如果你有一个最常见"的计数器,你可以使用它.作为限制条件,您甚至可以添加一个 dummy 项目(如果您使用空字符串获得的是访问冲突).然后,在 TextChange 事件中从数据库中读取您需要的数据.

On load you may populate the TextBox with a sub-set of your data (that can be even cached for future/shared use). If you have a "most common" counter you can use it. As limit condition you may even add a dummy item (if what you get with an empty string is an access violation). Then, on the TextChange event read the data you need from the database.

我只有一个问题:您不想在用户开始输入之前填充源代码吗?如果存在网络流量问题,那么您会移动大量数据.如果您移动大量数据,那么您的用户在开始输入内容时将不得不等待.可以接受吗?另一方面,如果他们不等待太多,则数据流可能不是那么大,您可以将该逻辑放在表单构造函数中的 BackgroundWorker 中(或者离那个时间不远).

I have only one question: you do not want to populate the source until the user starts to type? If there's the problem of network traffic then you move a lot of data. If you move a lot of data then your users will have to wait when they start to type something. Is it acceptable? On the other side if they do not wait too much maybe data stream is not so big and you can put that logic in a BackgroundWorker in the constructor of your form (or not far from that time).

这篇关于文本框自动完成无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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