daily pastebin goal
6%
SHARE
TWEET

DetermineDropDownWidth

a guest Sep 28th, 2017 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Code taken from:
  2. # https://stackoverflow.com/questions/4448509/how-to-make-the-combobox-drop-down-list-resize-itself-to-fit-the-largest-item/4811033#4811033
  3. #
  4. # With minor edits
  5. # - to take into account the with of the groupname
  6. # - Width +35 instead of +15, gives better results
  7. #
  8. # 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.
  9.  
  10. #GroupedComboBox.cs
  11.     private void DetermineDropDownWidth()
  12.     {
  13.  
  14.         int widestStringInPixels = 0;
  15.         foreach (Object o in Items)
  16.         {
  17.             string toCheckGrp = null;
  18.             string toCheck;
  19.             PropertyInfo pinfo;
  20.             Type objectType = o.GetType();
  21.             if (this.DisplayMember.CompareTo("") == 0)
  22.             {
  23.                 toCheck = o.ToString();
  24.             }
  25.             else
  26.             {
  27.                 pinfo = objectType.GetProperty(this.DisplayMember);
  28.                 toCheck = pinfo.GetValue(o, null).ToString();
  29.  
  30.                 pinfo = objectType.GetProperty(this.GroupMember);
  31.                 toCheckGrp = pinfo.GetValue(o, null).ToString();
  32.             }
  33.             if (TextRenderer.MeasureText(toCheck, this.Font).Width > widestStringInPixels)
  34.                 widestStringInPixels = TextRenderer.MeasureText(toCheck, this.Font).Width;
  35.             Font grpFont = new Font(Font,FontStyle.Bold);
  36.             if (TextRenderer.MeasureText(toCheckGrp, this.Font).Width > widestStringInPixels)
  37.                 widestStringInPixels = TextRenderer.MeasureText(toCheckGrp, grpFont).Width;
  38.         }
  39.         this.DropDownWidth = widestStringInPixels + 35;
  40.  
  41. # GroupedComboBox.cs
  42. # Needs to be called when setting DataSource
  43.             if (value != null)
  44.             {
  45.                 // wrap the object in a binding source and listen for changes
  46.                 ...
  47.                 DetermineDropDownWidth();
  48.             }
  49.             else
  50.             ...
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top