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

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

问题描述

我想实现在一个阵列,显示单字符输入的建议的形式自定义数据源的文本框自动完成。但是,当我运行程序自动完成第二只字符输入后开始。当我调试我看到的数据都是那里的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.

这code我已经写在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;

但它简化版,工作,直到第二个字符输入。
然而,如果代替阵列I初始化AutoCompleteNameCollection包含空字符串:namesCollection.Add();它为单字符输入但有些时候工作,它给AccessViolationException:尝试读取或写入受保护的内存。
那么,有什么办法可以解决这个问题?

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?

推荐答案

在负荷可能填充文本框与子集的数据(即可以更缓存未来/共享使用)。如果你有一个最常见的柜台,你可以使用它。作为限制条件,您甚至可以添加的虚拟的项目(如果你得到一个空字符串是什么访问冲突)。
然后,在 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天全站免登陆