I need a c# program ( search algorithm ), ie, When user types a character, the display should update to show all valid choices for the next character
and a list of possible matching stations.
Ex: User Input: D A R T, it should display DARTFORD and DARTMOUTH
Suggested outline is below:
public class Suggestions
{
HastSet nextLetters { get; set; }
HashSet stations { get; set; }
}
public class StationFinder
{
private static String[] stations = new String[] {"LIVERPOOL LIME STREET", "BIRMINGHAM NEW STREET", "KINGSTON", " DARTFORD", "DARTMOUTH" };
public Suggestions GetSuggestions( String userInput )
{
// TODO Compute result
Suggestions result = new Suggestions();
return result;
}
}
Note: The above code snippet is a suggestion, it can be modified if wish.
Regards,
Vishnu
ShawnPosted Jun 23, 2012, 9:56 AM
public partial class Form1 : Form
{
private static String[] stations = new String[] { "LIVERPOOL LIME STREET", "BIRMINGHAM NEW STREET", "KINGSTON", " DARTFORD", "DARTMOUTH", "BIRMINGHAM NEW STREET"};
public Form1()
{
InitializeComponent();
HashSet
foreach(string s in stations)
{
stations_Hash.Add(s);
}
foreach (string sh in stations_Hash)
{
comboBox1.AutoCompleteCustomSource.Add(sh.Trim());
}
}
}
Notice I added an extra "BIRMINGHAM NEW STREET" to the array. However, it will only show up once in the autocomplete.
I guess the real question is what controls are you wanting to use to display the data? Are you forced to use a text box maybe? I suppose if I understood the circumstances a little more, I might be more helpful. I hope this helps.
Vishnu ReghuPosted Jun 22, 2012, 1:12 PM
ShawnPosted Jun 21, 2012, 11:10 PM
private static String[] stations = new String[] {"LIVERPOOL LIME STREET", "BIRMINGHAM NEW STREET", "KINGSTON", " DARTFORD", "DARTMOUTH" };
I added that in the class, but outside all methods so it was global. I changed the AutoCompleteMode of the combobox to "Suggest" and the AutoCompleteSource property to "CustomSource". Last I added a few lines of code to the form's constructor so it looked like this:
One thing to note wsa that you have a space before DARTFORD. Didn't know if that was intentional, but I added .Trim() which took care of that. Anyway, I hope that helps. Always nice to accomplish something in just a few lines of code.