0

I have seen this question come up in a couple forums and threads but i have never seen anyone provide code to solve the question, just links to places to get toolkits that may or may not be worth dealing with.

I wanted to put a more current question with the results of my work. See my answer below.

ARidder101
  • 315
  • 2
  • 6
  • 16
  • Any reason you posted [your other answer](http://stackoverflow.com/a/41705517/559745) again here? – Floern Jan 17 '17 at 19:49
  • So in case people don't look at that question because it is fairly old – ARidder101 Jan 17 '17 at 19:50
  • Just because it's old doesn't mean no one looks at it. The other question has 62760 views, so there are many people looking at it (most likely visiting via Google). – Floern Jan 17 '17 at 19:54
  • If it is against the rules to post an informational thread then i will delete this asap but I figured it was a good thing to do especially since the OP appears to not have an account anymore to accept it so people know it works. – ARidder101 Jan 17 '17 at 19:56
  • Duplicating content over multiple questions has no use. It's better to have everything in one question. Of course the OP doesn't exist anymore, but that doesn't prevent other people to vote on your answer if they find it useful. – Floern Jan 17 '17 at 20:00
  • Like I said if I broke a rule or something let me know and I will delete this. I just wanted to make sure people could see what I came up with on a thread that wants diluted with numerous answers that I didn't find useful. – ARidder101 Jan 17 '17 at 20:05

1 Answers1

0

First you need a handler for your normal TextChanged event handler for the TextBox:

    private bool InProg;
    internal void TBTextChanged(object sender, TextChangedEventArgs e)
                {
                var change = e.Changes.FirstOrDefault();
                if ( !InProg )
                    {
                    InProg = true;
                    var culture = new CultureInfo(CultureInfo.CurrentCulture.Name);
                    var source = ( (TextBox)sender );
                    if ( ( ( change.AddedLength - change.RemovedLength ) > 0 || source.Text.Length > 0 ) && !DelKeyPressed )
                        {
                         if ( Files.Where(x => x.IndexOf(source.Text, StringComparison.CurrentCultureIgnoreCase) == 0 ).Count() > 0 )
                            {
                            var _appendtxt = Files.FirstOrDefault(ap => ( culture.CompareInfo.IndexOf(ap, source.Text, CompareOptions.IgnoreCase) == 0 ));
                            _appendtxt = _appendtxt.Remove(0, change.Offset + 1);
                            source.Text += _appendtxt;
                            source.SelectionStart = change.Offset + 1;
                            source.SelectionLength = source.Text.Length;
                            }
                        }
                    InProg = false;
                    }
                }

Then make a simple PreviewKeyDown handler:

private static bool DelKeyPressed;
internal static void DelPressed(object sender, KeyEventArgs e)
{ if ( e.Key == Key.Back ) { DelKeyPressed = true; } else { DelKeyPressed = false; } }

In this example "Files" is a list of directory names created on application startup.

Then just attach the handlers:

public class YourClass
  {
  public YourClass()
    {
    YourTextbox.PreviewKeyDown += DelPressed;
    YourTextbox.TextChanged += TBTextChanged;
    }
  }

With this whatever you choose to put in the List will be used for the autocomplete box. This may not be a great option if you expect to have an enormous list for the autocomplete but in my app it only ever sees 20-50 items so it cycles through very quick.

ARidder101
  • 315
  • 2
  • 6
  • 16