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

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

问题描述

我正在在C#中的自动建议/完成的文本框,我也跟着下面的链接,但文本框中显示未启用的建议

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

<一个href=\"http://stackoverflow.com/questions/5648476/how-to-create-autosuggest-textbox-in-windows-forms\">How在Windows窗体创建文本框自动提示?

//-------- 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;

下面是我的code,它是在winform的负载功能。而nameCollection initializtion在构造函数中......好心请帮助,使之工作。

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.

我编辑我的职务而不是创造新的......我试图在单行文本框中的我自己的code和它的工作。现在,我想在多线一样的...对于研究我GOOGLE了超过2天尝试不同的codeS(一个与INTELLI感),但它没有工作作为文本提供自动建议。任何一个可以给我建议,code的整个过程,以多行。谢谢。

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.

推荐答案

<一个href=\"http://msdn.microsoft.com/en-us/library/system.windows.forms.textbox.autocompletesource.aspx\">AutoCompleteSource不能在多行文本框控件工作。

至极意味着你需要从头开始吧:

Wich means you need to make it from scratch:

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

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

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

您需要在你的文本框eventhandling然而,这是一个有点粗糙,所以我会重写它停止在某一点上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();

    }
}

现在你需要的是一个处理程序来设置您的文本框中的文本:

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();                
        }
    }

放入null检查适当

Put in null checks where appropriate

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

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