0

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

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" };

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

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

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?

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • same as while Typing "A" then next "s" , all firstletter "As" will sort and remain in the listbox? –  Mar 02 '13 at 05:00
  • You mean something like an autocomplete menu from intellisense..? Look into either [FastColoredTextBox](http://www.codeproject.com/Articles/161871/Fast-Colored-TextBox-for-syntax-highlighting) or [AvalonEdit](http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor) – TtT23 Mar 02 '13 at 05:11
  • yes exactly .@about the links i cant use any reference within this project .so i need codes . –  Mar 02 '13 at 05:20
  • @Elegaic Implementing Autocomplete from scratch is no easy feat and is a huge material that needs to be discussed in detail. Look at the source code of those projects in how those projects implements autocomplete and that should give you an idea of how to go about it. – TtT23 Mar 02 '13 at 05:24
  • i think its just a matter of condition code? since i already have the highlights code same with the label and strings ... –  Mar 02 '13 at 05:38

1 Answers1

0

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,

  • What happens if the user hits ESC/Enter while typing something in?
  • What happens if the user loses focus on the richtextbox?
  • What about handling up and down arrow keys while the listbox is open?
  • Performance issue when there are tons of items to be searched for in the Listbox?

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.

TtT23
  • 6,876
  • 34
  • 103
  • 174
  • i just got problems regarding with it .both avalon and fastcoloredtextbox has a custom tool and reference w/c i cant found how the code works to do it... :( –  Mar 02 '13 at 08:14