greatmajestics
greatmajestics

Reputation: 1093

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.

Upvotes: 11

Views: 15780

Answers (5)

helgeheldre
helgeheldre

Reputation: 1101

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

Upvotes: 13

izzet
izzet

Reputation: 1

Thismay help you solving problem ; You can change table name. you can change the query to load listbox.

    ListBox lbox;
    private void IletisimBilgileriDoldur()
    {
        try
        {
            string strQuery= "Select adres From tblIletisimBilgileri Where adres <> '';";
            veri = new OleDbCommand(strQuery,strConn);
            veri.CommandType = CommandType.Text;
            if (strConn.State == ConnectionState.Closed) strConn.Open();
            oku = veri.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(oku);
            oku.Close();
            txtAdres.AutoCompleteCustomSource.Clear();
            if (dt.Rows.Count >= 0)
            {
                lbox = new ListBox();
                for (int count = 0; count < dt.Rows.Count; count++)
                {
                    lbox.Items.Add(dt.Rows[count]["adres"].ToString());
                }
            }
            txtAdres.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtAdres.AutoCompleteSource = AutoCompleteSource.CustomSource;
            if (strConn.State == ConnectionState.Open) strConn.Close();
        }
        catch (Exception)
        {
            if (strConn.State == ConnectionState.Open) strConn.Close();
        }
    }

    private void txtAdres_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        var x = txtAdres.Left;
        var y = txtAdres.Top + txtAdres.Height;
        var width = txtAdres.Width;
        const int height = 120;

        lbox.SetBounds(x, y, width, height);
        lbox.KeyDown += lbox_SelectedIndexChanged;
        lbox.DoubleClick += lbox_DoubleClick;
        gbxAdres.Controls.Add(lbox);
        lbox.BringToFront();
        lbox.Show();
        ActiveControl = txtAdres;
    }

    void lbox_DoubleClick(object sender, EventArgs e)
    {
        txtAdres.Text = ((ListBox)sender).SelectedItem.ToString();
        lbox.Hide();
    }

Upvotes: 0

David D.
David D.

Reputation: 367

Bit of confusion on the "auto-suggestion" since that is basically auto-complete without the permission from the user to "complete" the text. Nevertheless here are a couple of links you might find helpful:

http://docs.jquery.com/UI/Autocomplete

Autocomplete functionality on a textarea

AutoComplete extender for multi-line Textbox

Scroll down on link #2, a user suggested a jquery solution and compare with link #1. You may find a solution.

Third link is from asp forums, similar question like yours was also answered by a link. You might want to check that out.

Upvotes: 0

Rahul Ranjan
Rahul Ranjan

Reputation: 1058

You need to add a New Component class by 'Adding New Item'. and then write the code for that class and then add that component wherever required..

Upvotes: 2

Rahul Ranjan
Rahul Ranjan

Reputation: 1058

Try this code as it works in my case:

  AutoCompleteStringCollection MyCollection = new AutoCompleteStringCollection();
                while (reader.Read())
                {
                    namesCollection.Add(reader.GetString(0));
                }
                reader.Close();
    descriptionBox.AutoCompleteMode = AutoCompleteMode.Suggest;
descriptionBox.AutoCompleteSource = AutoCompleteSource.CustomSource;    
                descriptionBox.AutoCompleteCustomSource = namesCollection;
                con.Close();

Please check if the reader is getting the desired records..:)

Upvotes: 1

Related Questions