分层架构中的自动填充文本框

时间:2016-10-30 17:42:36

标签: c# winforms autocomplete textbox layered

我想在我的数据库中创建一个自动完成textbox

我在分层架构(模型,DAL,BLL,演示文稿)中编写应用程序。

我已经使用arraylist创建了一个方法,该方法在数据库中读取并返回我的select命令,该命令正在填充(我已经在combobox上测试过了。)< / p>

但是当我尝试插入textbox时,没有任何反应......它没有显示建议。

我在论坛中寻找了一些内容,但我发现了一个图层的示例,因为我在图层中进行开发,所以我无法在AutoCompleteStringCollection中增加属性DAL以填充我的选择命令。

如果有人知道如何解决这个问题,请向我解释!

其他信息:我将winForm与C#和SQL Server一起使用。

2 个答案:

答案 0 :(得分:0)

我想你想说&#34;但是当我尝试在文本框中插入时,没有任何反应...... 不会显示消化。&#34; 好吧,我不能只在这里编码所有图层,但可以在你的DAL中建议创建一个返回列表的方法,然后在你的表单页面上提供这样的代码

 txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
 txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
 var autoCompleteCollection = new AutoCompleteStringCollection();
 autoCompleteCollection.AddRange(DAL.GetMethod().ToArray());
 textbox.AutoCompleteCustomSource = autoCompleteCollection;

答案 1 :(得分:0)

感谢您的帮助!! 我使用了你的建议,并做了一些小改动,它对我来说很好......

事实证明,唯一的问题是我的方法列表,一旦我更改了它,请执行 List&lt;字符串&gt; 事情变得更好。 谁想知道,我就是这样做的:

DAL LAYER:

public List<string> LoadList()
{
List<string> tagsList = new List<string>();

using (SqlConnection connection = new SqlConnection(ADados.StringDeConexao))
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT column FROM table";

using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
if (!reader.IsDBNull(0))
tagsList.Add(reader.GetString(0));
}
reader.Close();
}
connection.Close();
return tagsList;
}

演示层(事件TextChanged):

PedidoBLL pedido = new PedidoBLL();

txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection popula = new AutoCompleteStringCollection();
popula.AddRange(pedido.LoadList().ToArray());
txtName.AutoCompleteCustomSource = popula;

在BLL Layer中我只是调用并返回DAL方法LoadList ...