views:

24

answers:

0

I'm popping up a ListBox over a TextBox. I set ListBox.Parent to be the DataGrdiView that houses the TextBox (which is the edit control for a DataGridViewCell).

I add string items to ListBox.Items, then I set ListBox.Location, ListBox.Width, ListBox.Height, and ListBox.Visible (to true).

All of that works fine.

But I can't set focus to the ListBox. I want to allow the user to choose an item in the list using his keyboard (cursor keys, etc.), so I do ListBox.Focus(), but that has no effect. The focus remains on the TextBox.

Here's my code:

private void textBox_KeyPress(object _sender, KeyPressEventArgs _e) {
    if ('`' == _e.KeyChar) {
        DataGridViewCell cell = listBox.Tag as DataGridViewCell;
        DataGridView grid = cell.DataGridView;

        listBox.Items.Clear();
        listBox.Items.AddRange(mChoices.ToArray());

        listBox.Parent = grid;
        Rectangle r = grid.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true);
        listBox.Location = new Point(r.left, r.Top);
        listBox.Show();
        listBox.SelectedIndex = 0;
        listBox.Focus();

        _e.Handled = true;
    }
}