Sebachtian
Sebachtian

Reputation: 37

Autocomplete show when i type in any part of textbox?

i try to make an autocomplete show when i want type "march" or any words after "; ", image:

enter image description here

var source = new AutoCompleteStringCollection();
source.AddRange(new string[]
                            {
                    "January",
                    "February",
                    "March",
                    "April",
                    "May",
                    "June",
                    "July",
                    "August",
                    "September",
                    "October",
                    "November",
                    "December"
                            });
textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
textBox1.AutoCompleteCustomSource = source;

or like when i want to add tags while make this question.

Upvotes: 0

Views: 63

Answers (1)

Dave Smash
Dave Smash

Reputation: 3001

You'll have to get creative to do this using a standard textbox. When you type tags on Stack Overflow, each time you finish one, it pops into its own little box... you could create an event handler for textBox1_TextChanged to check for the ; character, and then assume if they typed one, they are done with the previous tag. It would look something like this:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.TextLength > 0)
    {
        if (textBox1.Text[textBox1.TextLength - 1] == ';')
        {
            MyCustomTagControl tag = new MyCustomTagControl(textBox1.Text);
            MyLayoutControl.Controls.Add(tag);
            textBox1.Text = "";
        }
    }
}

Here, we check to make sure there is at least one character. Then we check the last character to see if it is a semicolon. If it is, you pass the whole text string into a constructor for your custom tag (which is probably just a pretty little panel with a label and an "X" to remove unwanted tags) and you add the tag to some kind of layout control like a FlowLayoutPanel. Then you reset the textbox text so they can start typing the next tag.

Upvotes: 1

Related Questions