Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Code taken from:
- # https://stackoverflow.com/questions/4448509/how-to-make-the-combobox-drop-down-list-resize-itself-to-fit-the-largest-item/4811033#4811033
- #
- # With minor edits
- # - to take into account the with of the groupname
- # - Width +35 instead of +15, gives better results
- #
- # NOT yet tested with items names > group names. Due to the indent of the group name, an additional static value might need to be added.
- #GroupedComboBox.cs
- private void DetermineDropDownWidth()
- {
- int widestStringInPixels = 0;
- foreach (Object o in Items)
- {
- string toCheckGrp = null;
- string toCheck;
- PropertyInfo pinfo;
- Type objectType = o.GetType();
- if (this.DisplayMember.CompareTo("") == 0)
- {
- toCheck = o.ToString();
- }
- else
- {
- pinfo = objectType.GetProperty(this.DisplayMember);
- toCheck = pinfo.GetValue(o, null).ToString();
- pinfo = objectType.GetProperty(this.GroupMember);
- toCheckGrp = pinfo.GetValue(o, null).ToString();
- }
- if (TextRenderer.MeasureText(toCheck, this.Font).Width > widestStringInPixels)
- widestStringInPixels = TextRenderer.MeasureText(toCheck, this.Font).Width;
- Font grpFont = new Font(Font,FontStyle.Bold);
- if (TextRenderer.MeasureText(toCheckGrp, this.Font).Width > widestStringInPixels)
- widestStringInPixels = TextRenderer.MeasureText(toCheckGrp, grpFont).Width;
- }
- this.DropDownWidth = widestStringInPixels + 35;
- # GroupedComboBox.cs
- # Needs to be called when setting DataSource
- if (value != null)
- {
- // wrap the object in a binding source and listen for changes
- ...
- DetermineDropDownWidth();
- }
- else
- ...
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement