文本框自动完成(多行) [英] textbox auto complete (Multi Line)

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

问题描述

我正在用 C# 做一个自动建议/完整的文本框,我按照下面的链接,但文本框没有显示建议

I am making a auto suggestion / complete textbox in C#, i followed below link, but text box isnt showing the suggestions

如何在窗体中创建自动提示文本框?

//-------- Get all distinct description -----------------------------
OleDbCommand command = new OleDbCommand(Queries.qry16, Connection);
OleDbDataReader reader = command.ExecuteReader();

//--------- Storing ------------------------------------
while (reader.Read())
{
    namesCollection.Add(reader.GetValue(0).ToString());
}

//----------- Close after use ---------------------------------------
reader.Close();

//----------- Set the auto suggestion in description box ------------
descriptionBox.AutoCompleteMode = AutoCompleteMode.Suggest;
descriptionBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
descriptionBox.AutoCompleteCustomSource = namesCollection;

这是我的代码,它在winform的加载功能中.并且 nameCollection 初始化在构造函数中...请帮助使其正常工作.

Here is my code , it is in load function of winform. And the nameCollection initializtion is in constructor... kindly please help to make it working.

我正在编辑我的帖子而不是创建新帖子...我在单行文本框中尝试了我自己的代码并且它起作用了.现在我想在多行中使用相同的内容......为了研究,我用谷歌搜索了超过 2 天的时间尝试不同的代码(一个具有智能感知),但它没有作为文本框中可用的自动建议起作用.任何人都可以给我建议将整个过程编码为多行..谢谢.

I am editing my post rather then creating new... I have tried the my own code in single line textbox and it worked. Now i want the same in multi line... For research i googled more then 2 days trying different codes (one with intelli sense) but it didnt worked as auto suggestion available in textbox. Can any one give me suggestion to code the whole procedure to multi line.. Thank you.

推荐答案

AutoCompleteSource 不适用于多行 TextBox 控件.

这意味着你需要从头开始:

Wich means you need to make it from scratch:

我会制作一个列表框来显示自动完成的内容:

I would make a ListBox to display the content of your autocomplete:

var listBox = new ListBox();
Controls.Add(listBox);

您需要对文本框进行事件处理,但这有点粗糙,所以我会重写它以在某个时候停止 keyupevent:

You need eventhandling on your textbox however this is a bit crude, so i would rewrite it to stop the keyupevent at some point:

private void textBox_KeyUp(object sender, KeyEventArgs e)
{
    var x = textBox.Left;
    var y = textBox.Top + textBox.Height;
    var width = textBox.Width + 20;
    const int height = 40;

    listBox.SetBounds(x, y, width, height );
    listBox.KeyDown += listBox_SelectedIndexChanged;

    List<string> localList = list.Where(z => z.StartsWith(textBox.Text)).ToList();
    if(localList.Any() && !string.IsNullOrEmpty(textBox.Text))
    {
        listBox.DataSource = localList;
        listBox.Show();
        listBox.Focus();

    }
}

现在您只需要一个处理程序来设置 textBox 中的文本:

Now all you need is a handler to set the text in your textBox:

 void listBox_SelectedIndexChanged(object sender, KeyEventArgs e)
    {
        if(e.KeyValue == (decimal) Keys.Enter)
        {
            textBox2.Text = ((ListBox)sender).SelectedItem.ToString();
            listBox.Hide();                
        }
    }

在适当的地方进行空检查

Put in null checks where appropriate

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

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