Guest User

DetermineDropDownWidth

a guest
Sep 28th, 2017
81
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

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×