Managed controls in the Compact Framework don't expose all available functionality of the native controls they wrap. And that is understandable since the CF team wanted to make them fast and small. For example, ComboBox control doesn't implement the DropDown event that could be quite helpfull to store a previouse value because it should fire before the SelectedIndexChanged event. So I've decided to implement this event using the NativeWindow class I blogged about the other day.
The Windows CE SDK documentation states that there's a CBN_DROPDOWN notification message that is send to the parent of the ComboBox when the listbox of a combo box is about to become visible. This message is sent in the form of WM_COMMAND.
A quick trip to the winuser.h file to pull out the values for the CBN_DROPDOWN and WM_COMMAND and here's the ComboSubclass class that catches this message and raises the DropDown event:
public class ComboSubclass : NativeWindow
{
private const int WM_COMMAND = 0x111;
private const int CBN_DROPDOWN = 7;
public event EventHandler DropDown;
private ComboBox comboBox;
public ComboSubclass(ComboBox comboBox, Control parent)
{
this.comboBox = comboBox;
//Subclass parent form
this.AssignHandle(parent.Handle);
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_COMMAND)
{
// Make sure that's its our ComboBox
if (m.LParam == comboBox.Handle)
{
if (DropDown != null)
{
// Raise the event
DropDown(comboBox, null);
}
}
}
base.WndProc(ref m);
}
}
The usage of this class should be pretty straightforward:
comboSubclass = new ComboSubclass(comboBox1, this);
comboSubclass.DropDown += new EventHandler(comboSubclass_DropDown);
void comboSubclass_DropDown(object sender, EventArgs e)
{
prevIndex = ((ComboBox)sender).SelectedIndex;
}
Update: You can achieve the same goal in the CF v1 by utilizing ApplicationEx class from SDF and WinProcFilter I posted last year.