【问题标题】:Autocomplete textbox in layered architecture分层架构中的自动完成文本框
【发布时间】:2016-10-31 10:10:01
【问题描述】:

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

我正在使用分层架构(模型、DAL、BLL、Presentation)对我的应用程序进行编程。

我已经用arraylist 创建了一个方法,它读取并返回我在数据库中的选择命令,该命令正在填充(我已经在combobox 上进行了测试)。

但是当我尝试插入 textbox 时,什么都没有发生……它没有显示建议。

我在论坛中寻找了一些东西,但我只找到了一层的示例,并且由于我正在分层开发,我无法在我的 DAL 中增加属性 AutoCompleteStringCollection 以由我的选择命令填充。

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

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

【问题讨论】:

    标签: c# winforms autocomplete textbox layered


    【解决方案1】:

    我想你想说“但是当我尝试在文本框中插入时,什么都没有发生……它显示了建议。” 好吧,我不能只在这里对所有层进行编码,而是可以建议在您的 DAL 中创建一个返回 List 的方法,然后在您的表单页面上提供这样的代码

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

    【讨论】:

    • 好吧,我尝试这样做,但在“AutoCompleteCollection.AddRange()”处调用我一个错误,据说无法将模型转换为字符串
    • 尝试从你的 DAL 返回一个 List 并在你的表单中调用它,就像我提到的那样, autoCompleteCollection.AddRange(DAL.GetMethod().ToArray()); DAL.GetMethod() 应该返回一个 List.
    【解决方案2】:

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

    原来唯一的问题是我的方法列表,一旦我改变它做一个 List 事情变得更好。 对于谁想知道,这是我的做法:

    DAL 层:

    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 层中,我只是调用并返回 DAL 方法 LoadList...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-18
      • 2010-09-22
      • 2017-07-03
      • 2012-06-05
      • 2019-11-27
      • 2010-10-31
      相关资源
      最近更新 更多