-2

I have a ListBox and a list of all the English names.

OK, let's say that a "user" input letter "J". I want my ListBox to pick a few items (maybe 5) to display the results to the user.

I don't want when my user types "J" and the ListBox have to load every name starting with "J". All I want my ListBox to do is randomly load a few items results starting with letter J.

                List<string> DictionaryList = new List<string>().Take(5).ToList();

            //WEB 
            WebClient web = new WebClient();
            String html = web.DownloadString("http://www.EXAMPLE.org/Letter/J");
            MatchCollection m1 = Regex.Matches(html, @"<li>\s*(.+?)\s*</li>", RegexOptions.Singleline);

            foreach (Match m in m1)
            {
                string city = m.Groups[1].Value;
                DictionaryList.Add(city);
            }

ANOTHER QUESTION UPDATE:

How can I update my ListBox result when user add another letter, example("Ja"). When user input "Ja". I want to update my ListBox to do the same thing as the question above(randomly pick few result close to "Ja")

Thanks, Wan-Fai.

Wan Fai
  • 13
  • 7
  • Try https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx – MichaelMao Jul 11 '16 at 10:08
  • Then you must call "web.DownloadString(..." every time user press a key. Or download all possibilities and cache it local, use linq to search it, probably faster. –  Jul 11 '16 at 10:30
  • Is this a winforms or wpf project? – Clint Jul 11 '16 at 10:52

1 Answers1

0

Perhaps this will help you. Use start data:

var list = new List<string>();
string[] names = {"Jon","Julia","Josh","Jonson","Mai","Gordon"};
var input = "Jo";

select 2 elements beginning with "Jo"

var result = list.Where(n=>n.StartsWith(input)).Take(2);

select 2 random elements beginning with "Jo" use this

var result = list.Shuffle().Where(n=>n.StartsWith(input)).Take(2);

select 2 similar elements at "Jo" use this

var result = list
    .Where(n=>CalcLevenshteinDistance(n.Substring(0,input.Count()),input) >= input.Count()-input.Count()*0.5)
    .Take(2);
Community
  • 1
  • 1
Noneme
  • 816
  • 6
  • 22