Dynamic Filling of TextBox AutoComplete not working

G

Guest

hi guys,

i would like to use the new autocomplete properties of the textbox in the
..net-framework 2.0. i have to say, that this works really great, but i would
like to fill the autocompletestringcollection while i'm typing. the reason
is, that the data comes from a sql server database with thousands of records,
and therefore i can't populate the autocompletestringcollection while
inizializing (e.g. form_load).

i'm working on that problem since a while, but i didn't find any solution. i
made the code easier to understand and to reproduce for you guys, so you can
copy & paste that into your development studio.

what i use:

c#, .NET-Framework 2.0.50727, Windows.Forms

just take a new form, put a button and a textbox on it, change
textbox-properties to:

AutoCompleteMode: Suggest
AutoCompleteSource: CustomSource

and take the following code:


//this method generates AutoCompleteStringCollection with 100 random entries
public AutoCompleteStringCollection GetFilteredList(string prefixText) {

AutoCompleteStringCollection AutoCol = new AutoCompleteStringCollection();

Random random = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < 100; i++) {
char c1 = (char)random.Next(33, 127);
char c2 = (char)random.Next(33, 127);
char c3 = (char)random.Next(33, 127);
AutoCol.Add(prefixText + c1 + c2 + c3);

}

return AutoCol;

}


//Event Buttonclick
private void button1_Click(object sender, EventArgs e) {

//fill autocomplete textbox
lock (textBox1.AutoCompleteCustomSource.SyncRoot) {
textBox1.AutoCompleteCustomSource = GetFilteredList("A");
}

}


after compiling and starting you can press the button and type something in
the textbox what begings with an "A". that works fine - as expected.


but the main question is:

how can i fill/update a textbox autocompletestringcollection while i'm typing?


in the textchanged-event the code would look something like this:


private void textBox1_TextChanged(object sender, EventArgs e) {

//fill autocomplete textbox..
//SyncRoot-object is not locked at all, so it looks like that is not
important
lock (textBox1.AutoCompleteCustomSource.SyncRoot) {
textBox1.AutoCompleteCustomSource = GetFilteredList(textBox1.Text);
}

}



and that kind of works. it "only" crashes sometimes, and also bad: the first
letter you type does nothing with autosuggest. i tried also all keyevents
(previewkeydown, keydown, keypress, keyup) with several different
code-snippets including sendkeys.send & sendkeys.sendwait. nothing works so
far.

i hope somebody has/had the same problem and found a solution for this. i
want it to work like google-suggest or this sample from the
..net-atlas-project (
http://atlas.asp.net/quickstart/atlas/samples/controls/servercontrols1.aspx),
but local on the desktop with windows-forms.

thx in advance
Daniel

ps: this "random generating code" is only to visualize the problem, the real
data comes from a sql server database as i said before.

pps: random-string-generating-code taken and changed from the
asp.net-atlas-project:
http://atlas.asp.net/quickstart/atlas/doc/controls/default.aspx
http://atlas.asp.net/quickstart/atlas/samples/controls/servercontrols1.aspx
http://atlas.asp.net/quickstart/uti...s1.src&file=FilterService.asmx&lang=C#+Source
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top