First of all, I'm using
Visual Studio 2005, C#. So, I've got this ComboBox called cbPeople on a
Form, and when the user types in a value and presses enter, I run a
query and populate the combo box with selections, which works fine. My
problem is that once the query is run, I want the drop down to be DOWN
so that the user sees the selections and can choose one. So I use the
DroppedDown property and set it to true.
My problem is that after the drop down drops down, immediately it goes
back up again, and nothing I've managed to do has been able to keep it
down! Any suggestions will be whole-heartedly appreciated!
Here's the code of my KeyPress event where the issue lies.
private void cbPeople_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (System.Char)Keys.Enter)
{
cbPeople.DataSource = _oc.SearchContacts(cbPeople.Text);
cbPeople.DroppedDown = true;
}
}
I've tried doing a cbPeople.Focus() at various points, but nothing Iv'e done has gotten that box to stay dropped down!
Thank you!
Joshua
Loading
Joshua ChambersPosted May 15, 2008, 12:53 PM
Scott LyslePosted May 14, 2008, 11:10 PM
Keyup follows keypress and cleans up the drop down. You can pass along to the keyup event that you want to pop open the combobox. In this code I created a boolean variable (bPopCombo) and initialized it to false. In the key press event, if the user hit enter, I set the boolean to true. In the keyup event, I looked at the variable and if it is true, I pop open the combobox and then set the boolean back to false.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Junker
{
public partial class Form1 : Form
{
bool bPopCombo;
public Form1()
{
InitializeComponent();
bPopCombo = false;
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Orange");
comboBox1.Items.Add("Apple");
comboBox1.Items.Add("Peach");
comboBox1.DroppedDown = true;
}
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (System.Char)Keys.Enter)
{
comboBox1.Items.Add("Bananas");
bPopCombo = true;
}
}
private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
if (bPopCombo)
{
comboBox1.DroppedDown = true;
bPopCombo = false;
}
}
}
}