Hi,
I am trying populate AutoComplete box using a call to remote server that return JSON list with suggestions. I've tried to put some code into "TextChanged" event handler, but either box does not appear or disappears fast. I already have following:
AutoCompleteMode = Suggest;
AutoCompleteSource = CustomSource;
So then in my code I set AutoCompleteCustomSource = "my new source that contains the suggestion".
Following is a sample code based on what I've explained above:
private void myCombBox_TextChanged(object sender, EventArgs e) { string requestUrl = string.Format(@"http://suggestqueries.google.com/complete/search?client=firefox&q={0:s}", System.Uri.EscapeDataString(cbRequests.Text)); WebRequest request = WebRequest.Create(requestUrl); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); string content = new StreamReader(response.GetResponseStream()).ReadToEnd(); response.Close(); JavaScriptSerializer serializer = new JavaScriptSerializer(); dynamic result = serializer.Deserialize(content, typeof(object)); this.BeginInvoke((Action)delegate() { myCombBox.AutoCompleteCustomSource.Clear(); myCombBox.AutoCompleteCustomSource.AddRange((result[1] as object[]).Cast<string>().ToArray()); myCombBox.SelectionLength = 0; myCombBox.SelectionStart = cbRequests.Text.Length; }); }
If I remove myComboBox update part out of BeginInvoke, then at some point I get Memory corruption error which is most likely dues to threading/locking issues.
Any suggestions?
Thanks