在RichTextBox中键入单词时,将显示隐藏的ListBox [英] Hidden ListBox will appear while Typing Words in RichTextBox

查看:55
本文介绍了在RichTextBox中键入单词时,将显示隐藏的ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究texteditor,我想知道如何实现自动完成功能.

I'm working on texteditor and I'd like to know how to implement an autocomplete feature.

我在单独的类(KeyWord.cs)中有此字符串集合

I have this collection of strings in my separate class (KeyWord.cs)

public String[] keywords = { "abstract", "as", "etc." };
public String[] events = { "AcceptRejectRule", "AccessibleEvents", "etc.2" };

我已经在我的主窗体中的ListBox(lb)中输入了字符串,并且已经对其进行了排序:

that I already have input the Strings in ListBox (lb) located my mainform, which are already sorted:

lb = new ListBox();
Controls.Add(lb);
//lb.Visible = false;

KeyWord keywordsL = new KeyWord();
KeyWord eventsL = new KeyWord();
foreach (string str in keywordsL.keywords)
{
    lb.Items.Add(str);
}
foreach (string str in eventsL.events)
{
    lb.Items.Add(str);
}

和用作编辑器的RichTextBox(还带有Highlights选项)声明为rtb.

and the RichTextBox which served as the editor (with highlights option also) declared as rtb.

现在我担心的是,当我在RichTextBox(rtb)中输入字母"A"时,如何使其像"contexthint"一样,隐藏的列表框将出现在鼠标指针所在的位置,然后所有列表框中列出的字符串开头将出现"A".最后,当我从列表框中选择显示的字符串时,该字符串将被添加到RichTextBox中吗?

Now my concern was, how can I make it like its "contexthint" like when I type in letter "A" in RichTextBox(rtb), a hidden listbox will appear in the position where the mousepointer was there and then all the "A" in the beggining of strings listed in the listbox will appear. Finally, when I select the shown string from listbox, the string will be added in the in the RichTextBox?

推荐答案

实现此目的的简便方法是执行以下操作:

Easy way to implement this is to do something like this:

private List<string> autoCompleteList = new List<string>();

public Form1()
{
    autoCompleteList.Add("Items for the autocomplete");
}
...

private void textBox1_TextChanged(object sender, System.EventArgs e)
{
    listBox1.Items.Clear();
    if (textBox1.Text.Length == 0)
    {
        hideAutoCompleteMenu();
        return;
    }

    Point cursorPt = Cursor.Position;
    listBox1.Location = PointToClient(cursorPt);

    foreach (String s in autoCompleteList)
    {
        if (s.StartsWith(textBox1.Text))
        {
            listBox1.Items.Add(s);
            listBox1.Visible = true;
        }

    }
 }

private void hideAutoCompleteMenu()
{
    listBox1.Visible = false;
}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
    textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
    hideAutoCompleteMenu();
}

但是,在实现此功能时,您必须考虑所有可能的极端情况,例如

However, you have to consider all the possible corner cases while implementing this feature such as,

  • 如果用户在键入内容时按ESC/Enter会发生什么?
  • 如果用户对富文本框失去关注会怎样?
  • 打开列表框时如何处理上下箭头键?
  • 在列表框中要搜索大量商品时的性能问题?

虽然上面的一些问题只是处理其他事件的问题,但是上面显示的代码是实现所需内容的一种非常快速且肮脏的方法,但实际上您似乎在做着什么工作的重新发明已经可用.我建议您查看AvalonEdit和FastColoredTextBox中的源代码,以了解这是如何完成的.

While some of the above issues is just a matter of handling additional events, the code shown above is a very quick and dirty way to implement what you want, but really it seems like what you are doing is reinventing the wheel for what's already available. I suggest you look at the source code in AvalonEdit and FastColoredTextBox to see how this is REALLY done.

这篇关于在RichTextBox中键入单词时,将显示隐藏的ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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