public override Size CalcRequiredDropDownSize() { var size = base.CalcRequiredDropDownSize(); if ((DropDownControl != null) && (Items.Count > 0)) { int checkSize = 0; if (Application.RenderWithVisualStyles) { using (Graphics graphics = m_ListBox.CreateGraphics()) checkSize = (int)(CheckBoxRenderer.GetGlyphSize(graphics, CheckBoxState.CheckedNormal)).Width; } else { checkSize = 16; // TODO } checkSize += 4; size.Height = Items.Count * Math.Max(m_ListBox.ItemHeight, checkSize); for (int index = 0; index < Items.Count; index++) { var text = Items[index].ToString(); if (!string.IsNullOrEmpty(text)) { int itemWidth = TextRenderer.MeasureText(text, m_ListBox.Font).Width; size.Width = Math.Max(itemWidth + checkSize, size.Width); } } } return(size); }
internal static void UpdateFieldsCheckedListBoxColumnWidth(CheckedListBox checkedListBox) { int num = 0; using (Graphics graphics = checkedListBox.CreateGraphics()) { foreach (object obj2 in checkedListBox.Items) { string text = obj2.ToString(); num = Math.Max(num, (int)graphics.MeasureString(text, checkedListBox.Font).Width); } } num += 50; checkedListBox.ColumnWidth = num; }
/// <summary> /// Edits the specified object's value using the editor style indicated by the /// System.Drawing.Design.UITypeEditor.GetEditStyle() method</summary> /// <param name="context">An System.ComponentModel.ITypeDescriptorContext that can be used to gain /// additional context information</param> /// <param name="provider">An System.IServiceProvider that this editor can use to obtain services</param> /// <param name="value">The object to edit</param> /// <returns>The new value of the object. If the value of the object has not changed, this should /// return the same object it was passed.</returns> public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { m_editorService = provider.GetService(typeof(IWindowsFormsEditorService)) as IWindowsFormsEditorService; if (m_editorService != null) { CheckedListBox checkedListBox = new CheckedListBox(); checkedListBox.CheckOnClick = true; foreach (string displayName in m_displayNames) { checkedListBox.Items.Add(displayName); } // size control so all strings are completely visible using (System.Drawing.Graphics g = checkedListBox.CreateGraphics()) { float width = 0f; foreach (string displayName in m_displayNames) { float w = g.MeasureString(displayName, checkedListBox.Font).Width; width = Math.Max(width, w); } float height = m_displayNames.Length * checkedListBox.ItemHeight; int scrollBarThickness = SystemInformation.VerticalScrollBarWidth; if (height > checkedListBox.Height - 4) // vertical scrollbar? { width += SystemInformation.VerticalScrollBarWidth; } if (width > checkedListBox.Width) { checkedListBox.Width = (int)width + 31; // magic number from Windows.Forms dll } } if (value is string) { FillCheckedListBoxFromString(value, checkedListBox); } else if (value is int || value is uint) { FillCheckedListBoxFromInt(value, checkedListBox); } // otherwise, ignore value m_editorService.DropDownControl(checkedListBox); object newValue; if (value is string) { newValue = ExtractStringFromCheckedListBox(checkedListBox); } else { newValue = ExtractIntFromCheckedListBox(checkedListBox); } // be careful to return the same object if the value didn't change if (!newValue.Equals(value)) { value = newValue; } } return(value); }