3 Replies - 696 Views - Last Post: 20 August 2014 - 08:20 AM Rate Topic: -----

#1 Mercifies  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-August 14

C# Problems with building Intellisense from scratch

Posted 17 August 2014 - 08:59 PM

So, I've got some problems with this intellisense feature I'm making for an editor, and it sort of works but has some obvious problems. I've got 2 methods to handle this, the first is keyDown which controls when the listbox should popup, and the second is keyArraySeach which is supposed to be passed a generic key which will be used to narrow down the listbox options. For example, when you press o it should remove all options not beginning with the letter o, then op limits futher, so on and so forth. It doesn't do this, although I am able to use the arrow keys and enter button to select which option and insert it with enter. I cannot select with the cursor, and once the listbox has been selected via cursor, the arrow keys and enter go dead until you select the textbox again, at which point they are usable. Here's my code:

keyDown
if (e.Shift && e.KeyValue == 188) // shift + comma
                {

                    // Find the position of the caret
                    Point point = this.textBox1.GetPositionFromCharIndex(textBox1.Selectionstart);
                    point.Y += (int)Math.Ceiling(this.textBox1.Font.GetHeight()) + 2;
                    point.X += 2; // for Courier, may need a better method

                    list.Location = new Point(point.Y, point.X);
                    list.Height = textBox1.Height / 3;
                    list.SelectedIndex = 0;
                    list.BringToFront();
                    list.Visible = true;
                    list.Parent = textBox1;
                    list.Show();

                    cursorIndex = textBox1.Selectionstart;

                }
                else if (list.Visible == true)
                {
                    if (e.KeyData == Keys.Down && list.SelectedIndex < list.Items.Count - 1) // up arrow control
                    {
                        list.SelectedIndex += 1;

                    }
                    else if (e.KeyData == Keys.Up && list.SelectedIndex > 0) // down arrow control
                    {
                        list.SelectedIndex -= 1;
                    }
                    else if (e.KeyData == Keys.Enter) // enter inserts currently selected index
                    {
                        int selectState = list.SelectedIndex;

                        if (selectState < list.Items.Count && selectState >= 0)
                        {
                            string temptext = list.Items[selectState].ToString();

                            textBox1.Paste(list.Items[selectState].ToString().Substring(1));
                            

                        }
                        else
                        {
                            textBox1.Paste(list.Items[0].ToString().Substring(1));
                        }

                        list.Visible = false;
                        textBox1.Text = textBox1.Text.Trim();
                    }
                    else if (e.Shift && e.KeyValue == 190)
                    {
                        list.Visible = false;
                    }
                    else if (e.KeyData == Keys.Back) // back space control
                    {
                        if (textBox1.Selectionstart <= cursorIndex + 1) // closes list if user deletes the < that opens list
                        {
                            list.Visible = false;
                        }
                    }
                    else if (e.KeyData == Keys.Escape) // closes list via escape
                    {
                        list.Visible = false;
                    }

                    else
                    {
                        keyArraySearch(e.KeyData); // calls method to refine search
                    }


                }


keyArraySeach (NOTE: I use lists for removing and adding to the listbox, the arrays are just permanent lists of all of the options. They are used to reload all tags later)
private void keyArraySearch(Keys a) // not currently working
        {
            List<string> fullList = new List<string>();
            List<string> tempList = new List<string>();

            char theKey = (char)a;
            string s = "<" + theKey;

            for (int i = 0; i < listTags.Length; i++) 
            {
                fullList.Add(listTags[i]);

                //refines list to items typed
                if (fullList[i].StartsWith(s)) //currently not working
                {
                    tempList.Add(fullList[i]); //
                }
            }

            if (tempList.Count > 0) //catches error for templist not loading
            {
                list.Items.Clear();
            }

            

            for (int i = 0; i < tempList.Count; i++)
            {
                list.Items.Add(tempList[i]);
            }

        }

This post has been edited by macosxnerd101: 17 August 2014 - 09:24 PM
Reason for edit:: Removed obnoxious size tags. Please be cognizant of those reading your thread.


Is This A Good Question/Topic? 0
  • +

Replies To: C# Problems with building Intellisense from scratch

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5925
  • View blog
  • Posts: 20,262
  • Joined: 05-May 12

Re: C# Problems with building Intellisense from scratch

Posted 17 August 2014 - 09:57 PM

The reason why type additional letters does not work is because you are using a local variable within your method keyArraySearch(). It has no memory of the previous keys the user had typed in.

The reason why the arrow keys stop working after selecting the listbox via the cursor is because focus has shifted.
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: C# Problems with building Intellisense from scratch

Posted 18 August 2014 - 06:30 AM

Personally I'd do some reasearch into "auto fill" and "auto complete" textboxes.
All the logic and behavior has been figured out. Just remember that in most cases, anything you want to make has already been needed by someone else.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Problems with building Intellisense from scratch

Posted 20 August 2014 - 08:20 AM

Along the same lines that tlhIn`toq mentioned, here's an open source editor control that, if you don't want to use, you can at least learn a few things from their code (the joys of open source!):

http://scintillanet.codeplex.com/
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1