public class ComboBoxExtender
{
private ComboBox comboBox;
private IntPtr handle;
public ComboBoxExtender(ComboBox comboBox)
{
this.comboBox = comboBox;
// Get native handle
this.comboBox.Capture = true;
this.handle = GetCapture();
this.comboBox.Capture = false;
}
public int DropDownWidth
{
get
{
return GetDropDownWidth();
}
set
{
SetDropDownWidth(value);
}
}
private void SetDropDownWidth(int width)
{
SendMessage(handle, CB_SETDROPPEDWIDTH, width, 0);
}
private int GetDropDownWidth()
{
return SendMessage(handle, CB_GETDROPPEDWIDTH, 0, 0);
}
#region P/Invokes
const int CB_GETDROPPEDWIDTH = 0x015f;
const int CB_SETDROPPEDWIDTH = 0x0160;
[DllImport("coredll.dll")]
static extern IntPtr GetCapture();
[DllImport("coredll.dll")]
static extern int SendMessage(IntPtr hwnd, int msg, int wParam, int lParam);
#endregion
}
You would use this class:
private ComboBoxExtender comboExt;
…
comboExt = new ComboBoxExtender(comboBox1);
…
comboExt.DropDownWidth = 250;