Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For Winform Controls:</p> <pre><code>/// &lt;summary&gt; /// Returns whether the function is being executed during design time in Visual Studio. /// &lt;/summary&gt; public static bool IsDesignTime(this Control control) { if (LicenseManager.UsageMode == LicenseUsageMode.Designtime) { return true; } if (control.Site != null &amp;&amp; control.Site.DesignMode) { return true; } var parent = control.Parent; while (parent != null) { if (parent.Site != null &amp;&amp; parent.Site.DesignMode) { return true; } parent = parent.Parent; } return false; } /// &lt;summary&gt; /// Sets the DropDownWidth to ensure that no item's text is cut off. /// &lt;/summary&gt; public static void SetDropDownWidth(this ComboBox comboBox) { var g = comboBox.CreateGraphics(); var font = comboBox.Font; float maxWidth = 0; foreach (var item in comboBox.Items) { maxWidth = Math.Max(maxWidth, g.MeasureString(item.ToString(), font).Width); } if (comboBox.Items.Count &gt; comboBox.MaxDropDownItems) { maxWidth += SystemInformation.VerticalScrollBarWidth; } comboBox.DropDownWidth = Math.Max(comboBox.Width, Convert.ToInt32(maxWidth)); } </code></pre> <p>IsDesignTime Usage:</p> <pre><code>public class SomeForm : Form { public SomeForm() { InitializeComponent(); if (this.IsDesignTime()) { return; } // Do something that makes the visual studio crash or hang if we're in design time, // but any other time executes just fine } } </code></pre> <p>SetDropdownWidth Usage:</p> <pre><code>ComboBox cbo = new ComboBox { Width = 50 }; cbo.Items.Add("Short"); cbo.Items.Add("A little longer"); cbo.Items.Add("Holy cow, this is a really, really long item. How in the world will it fit?"); cbo.SetDropDownWidth(); </code></pre> <p>I forgot to mention, feel free to use these on Codeplex...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload