Can't get focus on dynamically created listbox in C#

c#, winforms

Solution

If you want the user to be able to continue typing in the textbox while still manipulating the selection of the listbox, then you will have to fake it: don't actually set focus to the listbox when you show it. Instead, change the selection of the listbox manually in the `KeyDown` event for the textbox. Something like this:

private void txtResults_KeyDown(object sender, KeyEventArgs e)
{
    string[] words = ((TextBox)sender).Text.Split(' ');
    string s = sampleWord.Text = words[words.Length - 1];

    if (e.KeyCode == Keys.OemPeriod)
    {
        ShowPopUpList(s);
        lst.SelectedIndex = 0;
    }
    else if (lst.Visible && e.KeyCode == Keys.Up)
    {
        // manipulate the selection on the listbox (move up)
        if (lst.SelectedIndex > 0) 
            lst.SelectedIndex -= 1;

        // eat the keypress so it textbox doesn't get it and move the cursor
        e.Handled = true; 
    }
    else if (lst.Visible && e.KeyCode == Keys.Down)
    {
        // manipulate the selection on the listbox (move down)
        if (lst.SelectedIndex < lst.Items.Count - 1) 
            lst.SelectedIndex += 1;

        // eat the keypress so it textbox doesn't get it and move the cursor
        e.Handled = true;
    }
    else if (lst.Visible && e.KeyCode == Keys.Enter)
    {
        // insert current list box selection into text box and hide the list
        txtResults.Text += lst.SelectedItem;
        txtResults.SelectionStart = txtResults.Text.Length;
        lst.Hide();
        // eat the keypress to prevent the textbox (and the form) from acting on it
        e.SuppressKeyPress = true;
        e.Handled = true;
    }
    else
    {
        lst.Hide();
    }
}

Problem

I have previously asked a question on how to Selecting dynamically created Listboxs items in C# which was answered. The problem now is that when the listbox gets the focus when popped up! I can't continue typing unless I either select an item or press Esc, which I have explicitly defined to set focus back to my `TextBox`. The irony is that I have a condition in my `KeyDown` event which says if the UP or Down arrow keys are pressed, set the focus on `ListBox` so that user can choose an item, but don't transfer the focus so that user can continue typing freely. Just like what we have on Visual Studio, when a user presses a dot, he is not blocked and forced to choose an item form the Intellisense list, but he can continue typing and or at any time use arrow keys UP or Down to select the proper item. I can't achieve the same functionality using the code below. How can I get this to work as mentioned? I need to say that using ActiveControl, and transferFocus, using `this.Focus()` prior to `lst.Focus()`, disabling and enabling textbox they all didn't work! ``` private void txtResults_KeyDown(object sender, KeyEventArgs e) { string[] words= ((TextBox)sender).Text.Split(' '); string s = sampleWord.Text = words[words.Length - 1]; if (e.KeyCode == Keys.OemPeriod) { ShowPopUpList(s); lst.Focus(); //This transfers the focus to listbox but then prevents user //from being able to type anymore unless he/she chooses an item! } else if (e.KeyCode == Keys.Down || e.KeyCode == Keys.Up) { lst.Focus();//doesnt work :-/ } else { lst.Hide(); txtResults.Focus(); } } ```

Original source