I have control (implemented C#, .Net 2.0) that inherits from combobox. It has filtering and other stuff. To keep UI right, when amount of items during filtering falls, drop down list changes its size to fit the number of items left (it is done by NativeMethods.SetWindowPos(...)).
Is there any way to check if drop down list is revealed up or down (literally) - not to check if it is open, it is open, but in which direction, upwards or downwards?
cheers, jbk
The ComboBox has two events (DropDown
and DropDownClosed
) that are fired when the dropdown part opens and closes, so you might want to attach handlers to them to monitor the state of the control.
Alternatively, there's also a boolean property (DroppedDown
) which should tell you the current state.
ComboBoxes open downwards or upwards depending on the space they have to open: if they have avaliable space below them they'll open downwards as usual, if not they'll open upwards.
So you simply have to check if they have space enough below them to know. Try this code:
void CmbTestDropDown(object sender, EventArgs e)
{
Point p = this.PointToScreen(cmbTest.Location);
int locationControl = p.Y; // location on the Y axis
int screenHeight = Screen.GetBounds(new Point(0,0)).Bottom; // lowest point
if ((screenHeight - locationControl) < cmbTest.DropDownHeight)
MessageBox.Show("it'll open upwards");
else MessageBox.Show("it'll open downwards");
}
So i found an answer:
Here we have both handles to combobox:
/// <summary>
/// Gets a handle to the combobox
/// </summary>
private IntPtr HwndCombo
{
get
{
COMBOBOXINFO pcbi = new COMBOBOXINFO();
pcbi.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(pcbi);
NativeMethods.GetComboBoxInfo(this.Handle, ref pcbi);
return pcbi.hwndCombo;
}
}
And to dropdownlist of combobox:
/// <summary>
/// Gets a handle to the combo's drop-down list
/// </summary>
private IntPtr HwndDropDown
{
get
{
COMBOBOXINFO pcbi = new COMBOBOXINFO();
pcbi.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(pcbi);
NativeMethods.GetComboBoxInfo(this.Handle, ref pcbi);
return pcbi.hwndList;
}
}
Now, we can get rectangles from handles:
RECT comboBoxRectangle;
NativeMethods.GetWindowRect((IntPtr)this.HwndCombo, out comboBoxRectangle);
and
// get coordinates of combo's drop down list
RECT dropDownListRectangle;
NativeMethods.GetWindowRect((IntPtr)this.HwndDropDown, out dropDownListRectangle);
now we can check:
if (comboBoxRectangle.Top > dropDownListRectangle.Top)
{
....
}