Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

textbox auto complete (Multi Line)

I am making a auto suggestion / complete textbox in C#, i followed below link, but text box isnt showing the suggestions

How to create autosuggest textbox in windows forms?

//-------- Get all distinct description -----------------------------
OleDbCommand command = new OleDbCommand(Queries.qry16, Connection);
OleDbDataReader reader = command.ExecuteReader();

//--------- Storing ------------------------------------
while (reader.Read())
{
    namesCollection.Add(reader.GetValue(0).ToString());
}

//----------- Close after use ---------------------------------------
reader.Close();

//----------- Set the auto suggestion in description box ------------
descriptionBox.AutoCompleteMode = AutoCompleteMode.Suggest;
descriptionBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
descriptionBox.AutoCompleteCustomSource = namesCollection;

Here is my code , it is in load function of winform. And the nameCollection initializtion is in constructor... kindly please help to make it working.

I am editing my post rather then creating new... I have tried the my own code in single line textbox and it worked. Now i want the same in multi line... For research i googled more then 2 days trying different codes (one with intelli sense) but it didnt worked as auto suggestion available in textbox. Can any one give me suggestion to code the whole procedure to multi line.. Thank you.

like image 454
greatmajestics Avatar asked Oct 19 '12 10:10

greatmajestics


People also ask

How will you create multi line text box?

Following steps are used to set the Multiline property of the TextBox: Step 1 : Create a textbox using the TextBox() constructor provided by the TextBox class. // Creating textbox TextBox Mytextbox = new TextBox(); Step 2 : After creating TextBox, set the Multiline property of the TextBox provided by the TextBox class.

What is multiline in TextBox?

A multiline text box control is a large text box that can display several lines of text or accept this text from user input. Text boxes are often linked to select value lookups and detailed menus. You can place a multiline text box control within a section control.

What is AutoComplete TextBox?

AutoComplete Textbox Represents a control that provides a textbox for user input and a drop-down that contains possible matches based on the input in the textbox. This will send the request to the server every time when the input has been changed in an Autocomplete textbox.


1 Answers

AutoCompleteSource does not work on multiline TextBox controls.

Wich means you need to make it from scratch:

I would make a ListBox to display the content of your autocomplete:

var listBox = new ListBox();
Controls.Add(listBox);

You need eventhandling on your textbox however this is a bit crude, so i would rewrite it to stop the keyupevent at some point:

private void textBox_KeyUp(object sender, KeyEventArgs e)
{
    var x = textBox.Left;
    var y = textBox.Top + textBox.Height;
    var width = textBox.Width + 20;
    const int height = 40;

    listBox.SetBounds(x, y, width, height );
    listBox.KeyDown += listBox_SelectedIndexChanged;

    List<string> localList = list.Where(z => z.StartsWith(textBox.Text)).ToList();
    if(localList.Any() && !string.IsNullOrEmpty(textBox.Text))
    {
        listBox.DataSource = localList;
        listBox.Show();
        listBox.Focus();

    }
}

Now all you need is a handler to set the text in your textBox:

 void listBox_SelectedIndexChanged(object sender, KeyEventArgs e)
    {
        if(e.KeyValue == (decimal) Keys.Enter)
        {
            textBox2.Text = ((ListBox)sender).SelectedItem.ToString();
            listBox.Hide();                
        }
    }

Put in null checks where appropriate

like image 58
helgeheldre Avatar answered Oct 23 '22 03:10

helgeheldre