views:

694

answers:

4

hi

I want to have a textbox control that it give suggest and append from a database in win application whit C# 2008 and LINQ .

I do it whit Combobox but I can't do it whit textbox .

How do I do it?

A: 

You could attach to the KeyDown event and then query the database for that portion of the text that the user has already entered. For example, if the user enters "T", search for things that start with "T". Then, when they enter the next letter, for example "e", search for things in the table that start with "Te".

The available items could be displayed in a "floating" ListBox, for example. You would need to place the ListBox just beneath the TextBox so that they can see the entries available, then remove the ListBox when they're done typing.

Michael Todd
First rule of programming : don't reinvent the wheel ;)
Thomas Levesque
True. Didn't realize there was an option for this. I don't do data binding, ever. (Started with .Net 1.0, tried to use some of the databinding features when they came out, but found them way too restrictive.)
Michael Todd
+1  A: 

Check out the AutoCompleteSource, AutoCompleteCustomSource and AutoCompleteMode properties.

textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection col = new AutoCompleteStringCollection();
col.Add("Foo");
col.Add("Bar");
textBox1.AutoCompleteCustomSource = col;

Note that the designer allows you to do that without writing any code...

Thomas Levesque
I want use from Database.Can you help me?I want to read from database for Suggest and AutoComplete
mohammad reza
@mohammad reza: You will have to write code using ADO.net to access DB.
P.K
just fill the list with the results of your query
Thomas Levesque
I don't know how too fill the list by my query . I write a query whi LINQ
mohammad reza
A: 

of course it depends on how you implement it but perhaps this is a good start:

using System.Windows.Forms;

public class AutoCompleteTextBox : TextBox {

    private string[] database;//put here the strings of the candidates of autocomplete
    private bool changingText = false;

    protected override void OnTextChanged (EventArgs e) {
        if(!changingText && database != null) {
            //searching the first candidate
            string typed = this.Text.Substring(0,this.SelectionStart);
            string candidate = null;
            for(int i = 0; i < database.Length; i++)
                if(database[i].Substring(0,this.SelectionStart) == typed) {
                    candidate = database[i].Substring(this.SelectionStart,database[i].Length);
                    break;
                }
            if(candidate != null) {
                changingText = true;
                this.Text = typed+candidate;
                this.SelectionStart = typed.Length;
                this.SelectionLength = candidate.Length;
            }
        }
        else if(changingText)
            changingText = false;
        base.OnTextChanged(e);
    }

}

I'm not sure this is working very well, but I think the base of this code is good enough.

CommuSoft
didn't realised that there were buit-in properties for this. I don't work that much with System.Windows.Forms, the last version where i used it was .Net Framework 1.1
CommuSoft
+1  A: 

This might not be the best way to do things, but should work:

 this.textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
 this.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;

private void textBox1_TextChanged(object sender, EventArgs e)
{
    TextBox t = sender as TextBox;
    if (t != null)
    {
        //say you want to do a search when user types 3 or more chars
        if (t.Text.Length >= 3)
        {
            //SuggestStrings will have the logic to return array of strings either from cache/db
            string[] arr = SuggestStrings(t.Text);

            AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
            collection.AddRange(arr);

            this.textBox1.AutoCompleteCustomSource = collection;
        }
    }
}
P.K
SuggestString don't allow to run program and say "The name 'SuggestStrings' does not exist in the current context"
mohammad reza
@mohammad-reza As the comment in the code says //SuggestStrings will have the logic to return array of strings either from cache/db.You will have to implement SuggestStrings. Don't expect that you will just copy code from SO and it will start working. We can just provide you with pointers.
P.K