I have a WinForms ComboBox within an Outlook Add-in which populates the drop-down with user names, when the user types names in the edit box. When the user reaches the third character. The data source of the ComboBox is assigned at this point, and the curser jumps to the beginning of the edit box instead of staying at the end.
The behavior I want is that the cursor stays at the end of the string I am typing, even when the drop-down is populated with more data.
I've tried hacking this with send keys, but it doesn't always work. The code which reads the keys below is in the key pressed event.
private void comboBox1_KeyPress(object sender, KeyEventArgs e)
{
var acceptableKeys = ConfigurationManager.AppSettings["AcceptableKeys"];
if (cmbAssignedTo.Text.Length > 2 && acceptableKeys.Contains(e.KeyCode.ToString().ToUpper()) && e.Modifiers == Keys.None)
{
var request = RestHandler.CreateRequest(ConfigurationManager.AppSettings["ContactsSearchResource"] + cmbAssignedTo.Text.Trim(), Method.GET);
var response = RestHandler.ExecuteRequest(request, ConfigurationManager.AppSettings["myServiceURL"]);
this.cmbAssignedTo.DataSourceChanged -= new System.EventHandler(this.cmbAssignedTo_DataSourceChanged);
//Assign a new data source
DataHandler.UpdateComboboxDataSource(cmbAssignedTo, response.Content);
this.cmbAssignedTo.DataSourceChanged += new System.EventHandler(this.cmbAssignedTo_DataSourceChanged);
}
e.Handled = true;
}
Edit
internal static void UpdateComboboxDataSource(ComboBox cmbAssignedTo, string data)
{
var list = BuildAssignmentList(data);
if ((list.Count() == 0 && cmbAssignedTo.Items.Count == 0) || list.Count() > 0)
{
var savedText = cmbAssignedTo.Text;
cmbAssignedTo.DataSource = list;
cmbAssignedTo.SelectedValue = "";
cmbAssignedTo.Text = savedText;
SendKeys.Send("{end}");
}
if (cmbAssignedTo.Items.Count > 0)
{
cmbAssignedTo.DroppedDown = true;
Cursor.Current = Cursors.Default;
}
}
I don't see how I can update the dropdown without changing the DataSource and that change appears to cause the cursor to jump. Should I try different event than KeyPressed? Is there some other solution I'm missing?