C# how to let textbox always keep in autocomplete?(Windows Form)
-
I use below code to reach the autocomplete in a textbox. But the chance is once, it cannot keep in autocomplete status. When I finish an autocomplete work, and then I cannot key in another autocomplete word. Does somebody know how to let textbox always keep in autocomplete?
private void Form1_Load(object sender, EventArgs e)
{
// Create the list to use as the custom source.
var source = new AutoCompleteStringCollection();
source.AddRange(new string[]
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
});// Create and initialize the text box. var textBox = new TextBox { AutoCompleteCustomSource = source, AutoCompleteMode = AutoCompleteMode.SuggestAppend, AutoCompleteSource = AutoCompleteSource.CustomSource, Location = new Point(20, 20), Width = ClientRectangle.Width - 40, Visible = true }; // Add the text box to the form. Controls.Add(textBox); }
-
I use below code to reach the autocomplete in a textbox. But the chance is once, it cannot keep in autocomplete status. When I finish an autocomplete work, and then I cannot key in another autocomplete word. Does somebody know how to let textbox always keep in autocomplete?
private void Form1_Load(object sender, EventArgs e)
{
// Create the list to use as the custom source.
var source = new AutoCompleteStringCollection();
source.AddRange(new string[]
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
});// Create and initialize the text box. var textBox = new TextBox { AutoCompleteCustomSource = source, AutoCompleteMode = AutoCompleteMode.SuggestAppend, AutoCompleteSource = AutoCompleteSource.CustomSource, Location = new Point(20, 20), Width = ClientRectangle.Width - 40, Visible = true }; // Add the text box to the form. Controls.Add(textBox); }
I think your problem is that the text isn't highlighted when you go back into the textbox, so any characters you start to type are appended to what is already there - hence auto-complete does not work. The solution is to highlight all of the text as you enter the textbox: Add this line just before you add textbox to the Controls collection
textBox.Enter += new System.EventHandler(this.textBox\_GotFocus);
and add this method to the form
private void textBox_GotFocus(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}Unfortunately when you use the mouse to enter the field the caret is positioned wherever the mouse pointer is so the MouseEnter method doesn't work in quite the same way as we'd like. To get over that you can use the MouseClick event:
textBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox_MouseClick);
and add this method
private void textBox_MouseClick(object sender, MouseEventArgs e)
{
((TextBox)sender).SelectAll();
} -
I think your problem is that the text isn't highlighted when you go back into the textbox, so any characters you start to type are appended to what is already there - hence auto-complete does not work. The solution is to highlight all of the text as you enter the textbox: Add this line just before you add textbox to the Controls collection
textBox.Enter += new System.EventHandler(this.textBox\_GotFocus);
and add this method to the form
private void textBox_GotFocus(object sender, EventArgs e)
{
((TextBox)sender).SelectAll();
}Unfortunately when you use the mouse to enter the field the caret is positioned wherever the mouse pointer is so the MouseEnter method doesn't work in quite the same way as we'd like. To get over that you can use the MouseClick event:
textBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.textBox_MouseClick);
and add this method
private void textBox_MouseClick(object sender, MouseEventArgs e)
{
((TextBox)sender).SelectAll();
}Hello CHill60! Thanks your answer. But my problem is that I cannot input the second word with Autocomplete list item. For example, when I input "May Apr", it should be disapeear "May April" for me to select "April". Now it just can work in frist word "May".
-
Hello CHill60! Thanks your answer. But my problem is that I cannot input the second word with Autocomplete list item. For example, when I input "May Apr", it should be disapeear "May April" for me to select "April". Now it just can work in frist word "May".