Search code examples
c#winformsintellisenseinputbox

Is it possible to have intellisense on InputBox


In my Tic Tac Toe game I ask the players for their names and I ask them whether they want it to be saved. The next time they start the game I want the InputBox to display some sort of auto complete or IntelliSense to appear.

QUESTION :

How do I get auto complete or IntelliSense to appear on InputBox

Just for in case. I included :

using Microsoft.VisualBasic;

in my code.


Solution

  • In order to use an "Intellisense" by which I'm guessing you mean "AutoComplete", you can simply set up a TextBox and assign its AutoComplete... properties. The ones you'll need in particular are:

    1. AutoCompleteCustomSource - Which is just a collection, so you can add your own list of Strings to use as a source. That way you wont have to worry about a DataSet or any kind of database access, if you don't want to go that way.
    2. AutoCompleteMode - Which tells the control what type of AutoComplete input box it should be.
    3. AutoCompleteSource - Which in your case, if you use #1 as the solution, you'd set this to CustomSource.

    edit

    As an example:

    textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
    textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    AutoCompleteStringCollection items = new AutoCompleteStringCollection();
    items.Add("Save");
    items.Add("Don't Save");
    
    textBox1.AutoCompleteCustomSource = items;
    

    edit 2

    As requested, here is how to read a text file line by line and use the data within as the custom source for your AutoComplete

    string line = "";
    AutoCompleteStringCollection items = new AutoCompleteStringCollection();
    
    // Read the file and display it line by line.
    System.IO.StreamReader file = new System.IO.StreamReader("YOURFILE.txt");
    while((line = file.ReadLine()) != null)
    {
        if(!String.IsNullOrEmpty(line))
        {
            items.Add(line);
        }
    }
    
    file.Close();
    

    Hope this helps!