protected override void OnDrawItem(DrawItemEventArgs e) { int index = e.Index; if (index > -1) { e.DrawBackground(); IEnableableComboBoxItem item = Items[index] as IEnableableComboBoxItem; Color textColor = SystemColors.ControlText; //Paint disabled items grey - otherwise leave them black if (item != null && !item.Enabled) { textColor = SystemColors.GrayText; } if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { textColor = SystemColors.HighlightText; } Drawing.DrawText(e.Graphics, Items[index].ToString(), Font, e.Bounds.Location, textColor); } base.OnDrawItem(e); }
private IntPtr ReplacementWndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) { if (msg == (uint)Win32.WM_LBUTTONDOWN || msg == (uint)Win32.WM_LBUTTONDBLCLK) { Win32.POINT loc = new Win32.POINT(); loc.X = MousePosition.X; loc.Y = MousePosition.Y; Win32.ScreenToClient(DropDownHandle, ref loc); Win32.RECT dropdown_rect = new Win32.RECT(); Win32.GetClientRect(DropDownHandle, out dropdown_rect); if (dropdown_rect.Left <= loc.X && loc.X < dropdown_rect.Right && dropdown_rect.Top <= loc.Y && loc.Y < dropdown_rect.Bottom) { int index = (int)Win32.SendMessage(DropDownHandle, Win32.LB_ITEMFROMPOINT, IntPtr.Zero, (IntPtr)(loc.X + (loc.Y << 16))); if (index >> 16 == 0) { Object o = Items[index]; IEnableableComboBoxItem enableableComboBoxItem = o as IEnableableComboBoxItem; if (enableableComboBoxItem != null && !enableableComboBoxItem.Enabled) { return(IntPtr.Zero); } } } } return(Win32.CallWindowProc(oldWndProc, hWnd, msg, wParam, lParam)); }
private void m_comboBoxConnection_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); ComboBox cb = (ComboBox)sender; int index = e.Index; if (index > -1 && cb != null) { IEnableableComboBoxItem item = cb.Items[index] as IEnableableComboBoxItem; using (SolidBrush textBrush = new SolidBrush(SystemColors.ControlText)) { //Paint disabled items grey - otherwise leave them black if (item != null && !item.Enabled) { textBrush.Color = SystemColors.GrayText; } if ((e.State & DrawItemState.Selected) == DrawItemState.Selected) { textBrush.Color = SystemColors.HighlightText; } e.Graphics.DrawString(cb.Items[index].ToString(), ((Control)sender).Font, textBrush, e.Bounds.X, e.Bounds.Y); } } }
protected override void OnSelectedIndexChanged(EventArgs e) { int i = SelectedIndex; if (i == -1 || AllItemsDisabled()) { return; } if (!(Items[i] is IEnableableComboBoxItem)) { base.OnSelectedIndexChanged(e); return; } while (true) { if (i == 0) { skip = true; } else if (i == Items.Count - 1) { skip = false; } IEnableableComboBoxItem item = Items[i] as IEnableableComboBoxItem; if (item != null && !item.Enabled) { i += skip ? 1 : -1; } else { skip = true; break; } } if (SelectedIndex != i) { //Calling this resends the event - you don't want to call the base //event or any registered handlers will be triggered twice SelectedIndex = i; return; } base.OnSelectedIndexChanged(e); }