Exemplo n.º 1
0
        private static int GetLargestTextExtent(System.Windows.Forms.ComboBox cbo)
        {
            int num = -1;

            if (cbo.Items.Count >= 1)
            {
                using (Graphics graphics = cbo.CreateGraphics())
                {
                    int num2 = 0;
                    if (cbo.Items.Count > cbo.MaxDropDownItems)
                    {
                        num2 = SystemInformation.VerticalScrollBarWidth;
                    }
                    for (int i = 0; i < cbo.Items.Count; i++)
                    {
                        int num3 = (int)graphics.MeasureString(cbo.Items[i].ToString(), cbo.Font).Width + num2;
                        if (num3 > num)
                        {
                            num = num3;
                        }
                    }
                }
            }
            return(num);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Resizes the combo box size horizontally to show all content strings
        /// http://rajeshkm.blogspot.com/2006/11/adjust-combobox-drop-down-list-width-c.html
        /// </summary>
        private static void SetComboBoxWidth(ComboBox box)
        {
            int width = box.Width;
            Graphics g = box.CreateGraphics();
            Font font = box.Font;

            // Checks if a scrollbar will be displayed.
            // If yes, then get its width to adjust the size of the drop down list.
            int vertScrollBarWidth =
                (box.Items.Count > box.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            // Loop through list items and check size of each items.
            // Set the width of the drop down list to the width of the largest item.
            foreach (string s in box.Items)
            {
                if (s != null)
                {
                    int newWidth = (int)g.MeasureString(s.Trim(), font).Width + vertScrollBarWidth;
                    if (width < newWidth)
                        width = newWidth;
                }
            }
            box.DropDownWidth = width;
        }
Exemplo n.º 3
0
 public static void ResizeComboBoxDropDownWidth(ComboBox comboBox, int minWidth, int maxWidth)
 {
     var calculatedWidth = 0;
     using (var graphics = comboBox.CreateGraphics())
     {
         foreach (object obj in comboBox.Items)
         {
             var area = graphics.MeasureString(obj.ToString(), comboBox.Font);
             calculatedWidth = Math.Max((int) area.Width, calculatedWidth);
         }
     }
     comboBox.DropDownWidth = Math.Min(Math.Max(calculatedWidth, minWidth), maxWidth);
 }
        // Calculates maximum required width of the dropdown portion for the ComboBox.
        // This is done in order not to cut off the longest dropdown item.
        public static int CalculateComboBoxDropdownWidth(ComboBox comboBox)
        {
            if (comboBox == null)
                return SystemInformation.VerticalScrollBarWidth;

            int scrollbarOffset = 0;

            if (comboBox.Items.Count > comboBox.MaxDropDownItems)
            {
                scrollbarOffset = SystemInformation.VerticalScrollBarWidth;
                try
                {
                    scrollbarOffset += SystemInformation.HorizontalFocusThickness; // padding
                }
                catch (NotSupportedException)
                {
                    scrollbarOffset += 2; // 2 pixels padding
                }
            }

            float maxWidth = 0.0f;
            using (System.Drawing.Graphics ds = comboBox.CreateGraphics())
            {
                foreach (object item in comboBox.Items)
                {
                    maxWidth = Math.Max(maxWidth, ds.MeasureString(comboBox.GetItemText(item), comboBox.Font).Width);
                }
            }

            int newWidth = (int)decimal.Round((decimal)maxWidth, 0) + scrollbarOffset;

            //If the width is bigger than the screen, ensure
            //we stay within the bounds of the screen
            if (newWidth > Screen.GetWorkingArea(comboBox).Width)
            {
                newWidth = Screen.GetWorkingArea(comboBox).Width;
            }

            // Don't let dropdown area to be outside of the screen
            if (comboBox.Parent != null)
            {
                int screenWidth = Screen.FromControl(comboBox).Bounds.Width;
                Point pt = comboBox.Parent.PointToScreen(new Point(comboBox.Left + newWidth, comboBox.Bottom));
                if (pt.X > screenWidth)
                    newWidth = Math.Max(comboBox.Bounds.Width, newWidth - (pt.X - screenWidth));
            }

            // Only change the width if the calculated width is larger that the current width
            return comboBox.Bounds.Width > newWidth ? comboBox.Bounds.Width : newWidth;
        }
Exemplo n.º 5
0
        public static void AutoSizeDropDown(ComboBox comboBox)
        {
            // Make the dropdown at least as wide as the combo box itself
            int widestWidth = comboBox.Width;

            using (Graphics g = comboBox.CreateGraphics())
            {
                foreach (object item in comboBox.Items)
                {
                    string valueToMeasure = item.ToString();

                    int currentWidth = TextRenderer.MeasureText(g, valueToMeasure, comboBox.Font).Width;
                    if (currentWidth > widestWidth)
                        widestWidth = currentWidth;
                }
            }

            comboBox.DropDownWidth = widestWidth;
        }
Exemplo n.º 6
0
        public static void ResizeDropDownListToMaxWidth(this ComboBox senderComboBox)
        {
            int      width = senderComboBox.DropDownWidth;
            Graphics g     = senderComboBox.CreateGraphics();
            Font     font  = senderComboBox.Font;
            int      vertScrollBarWidth =
                (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                ? SystemInformation.VerticalScrollBarWidth : 0;

            int newWidth;

            foreach (object item in senderComboBox.Items)
            {
                string s = senderComboBox.GetItemText(item);
                newWidth = (int)g.MeasureString(s, font).Width
                           + vertScrollBarWidth;
                if (width < newWidth)
                {
                    width = newWidth;
                }
            }
            senderComboBox.DropDownWidth = width;
        }
Exemplo n.º 7
0
        /// <summary>
        /// Calculates the width of the drop down list of the given combo box
        /// </summary>
        /// <param name="combo">Combo box to base calculate from</param>
        /// <returns>Width of longest string in combo box items</returns>
        /// <history>
        /// [Curtis_Beard]    11/21/2005	Created
        /// </history>
        private int CalculateDropDownWidth(ComboBox combo)
        {
            const int EXTRA = 10;

             Graphics g = combo.CreateGraphics();
             int _max = combo.Width;
             string _itemValue = string.Empty;
             SizeF _size;

             foreach (object _item in combo.Items)
             {
            _itemValue = _item.ToString();
            _size = g.MeasureString(_itemValue, combo.Font);

            if (_size.Width > _max)
               _max = Convert.ToInt32(_size.Width);
             }

             // keep original width if no item longer
             if (_max != combo.Width)
            _max += EXTRA;

             g.Dispose();

             return _max;
        }
Exemplo n.º 8
0
 public static void SetComboBoxDropDownWidth(ComboBox cbx)
 {
     //if (!cbx.IsHandleCreated || !(cbx.DataSource is DataTable)) { return; }
     //bool isDatabound = cbx.DataSource != null && cbx.DisplayMember != null && cbx.DisplayMember != "";
     int width = cbx.DropDownWidth,
         vScrollbarWidth = cbx.Items.Count > cbx.MaxDropDownItems ? SystemInformation.VerticalScrollBarWidth : 0;
     using (System.Drawing.Graphics g = cbx.CreateGraphics())
     {
         for (int i = 0; i < cbx.Items.Count; i++)
         {
             width = Math.Max((int)g.MeasureString(cbx.GetItemText(cbx.Items[i]), cbx.Font).Width + vScrollbarWidth, width);
         }
     }
     cbx.DropDownWidth = width;
 }
Exemplo n.º 9
0
 public static void InitializeTextEncodingComboBox(ComboBox comboBox)
 {
     var defaultEncoding = Configuration.Settings.General.DefaultEncoding;
     var selectedItem = (TextEncodingListItem)null;
     comboBox.BeginUpdate();
     comboBox.Items.Clear();
     using (var graphics = comboBox.CreateGraphics())
     {
         var maxWidth = 0.0F;
         foreach (var encoding in Configuration.AvailableEncodings)
         {
             if (encoding.CodePage >= 949 && !encoding.IsEbcdic())
             {
                 var item = new TextEncodingListItem(encoding);
                 if (selectedItem == null && item.Equals(defaultEncoding))
                     selectedItem = item;
                 var width = graphics.MeasureString(item.DisplayName, comboBox.Font).Width;
                 if (width > maxWidth)
                     maxWidth = width;
                 if (encoding.CodePage.Equals(Encoding.UTF8.CodePage))
                     comboBox.Items.Insert(0, item);
                 else
                     comboBox.Items.Add(item);
             }
         }
         comboBox.DropDownWidth = (int)Math.Round(maxWidth + 7.5);
     }
     if (selectedItem == null)
         comboBox.SelectedIndex = 0; // UTF-8 if DefaultEncoding is not found
     else
         comboBox.SelectedItem = selectedItem;
     comboBox.EndUpdate();
     Configuration.Settings.General.DefaultEncoding = (comboBox.SelectedItem as TextEncodingListItem).Encoding.WebName;
 }
Exemplo n.º 10
0
 public static void InitializeSubtitleFormatComboBox(ComboBox comboBox, IEnumerable<string> formatNames, string selectedName)
 {
     var selectedIndex = 0;
     comboBox.BeginUpdate();
     comboBox.Items.Clear();
     using (var graphics = comboBox.CreateGraphics())
     {
         var maxWidth = 0.0F;
         foreach (var name in formatNames)
         {
             var index = comboBox.Items.Add(name);
             if (name.Equals(selectedName, StringComparison.OrdinalIgnoreCase))
                 selectedIndex = index;
             var width = graphics.MeasureString(name, comboBox.Font).Width;
             if (width > maxWidth)
                 maxWidth = width;
         }
         comboBox.DropDownWidth = (int)Math.Round(maxWidth + 7.5);
     }
     comboBox.SelectedIndex = selectedIndex;
     comboBox.EndUpdate();
 }
Exemplo n.º 11
0
        private int GetIdealDropDownWidth( ICollection items, ComboBox c, int minimumWidth )
        {
            int width = minimumWidth;
            Graphics g = c.CreateGraphics();
            Font f = c.Font;
            int scrollBarWidth = (items.Count > c.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
            int itemWidth;
            foreach( object o in items )
            {
                string s = o.ToString();
                itemWidth = (int)g.MeasureString( s, f ).Width + scrollBarWidth;
                if( width < itemWidth )
                {
                    width = itemWidth;
                }
            }

            return width;
        }
Exemplo n.º 12
0
        /// <summary>
        /// Resizes a <code>System.Windows.Forms.ComboBox</code>'s DropDownWidth
        /// based on the longest string in the list.
        /// </summary>
        public static void ResizeDropDownForLongestEntry(ComboBox comboBox)
        {
            int width = comboBox.DropDownWidth;
            using (Graphics g = comboBox.CreateGraphics())
            {
                Font font = comboBox.Font;
                int vertScrollBarWidth = (comboBox.Items.Count > comboBox.MaxDropDownItems)
                    ? SystemInformation.VerticalScrollBarWidth : 0;
                int newWidth;

                foreach (object o in comboBox.Items)
                {
                    string s = "";

                    if (o is string)
                        s = (string)o;
                    else if (o is KeyValuePair<SvnDepth, string>)
                    {
                        s = ((KeyValuePair<SvnDepth, string>)o).Value;
                    }
                    else
                    {
                        return;
                    }

                    newWidth = (int)g.MeasureString(s, font).Width
                        + vertScrollBarWidth;
                    if (width < newWidth)
                    {
                        width = newWidth;
                    }
                }
                if (comboBox.DropDownWidth < width)
                    comboBox.DropDownWidth = width;
            }
        }
Exemplo n.º 13
0
        private int getComboBoxWith(ComboBox comboBox, string labelText)
        {
            Graphics graphics = comboBox.CreateGraphics();

            int max = Convert.ToInt32(graphics.MeasureString(labelText, comboBox.Font).Width);

            for (int i = 0; i < comboBox.Items.Count; i++)
            {
                int curWith = Convert.ToInt32(graphics.MeasureString(comboBox.Items[i].ToString(), comboBox.Font).Width);

                if (max < curWith)
                    max = curWith;
            }

            return max + 15;
        }
Exemplo n.º 14
0
            public static void DropDown(ComboBox comboBox)
            {
                if (comboBox.Tag != null)
                {
                  return;
                }

                int width = comboBox.DropDownWidth;
                Graphics g = comboBox.CreateGraphics();
                Font font = comboBox.Font;

                int vertScrollBarWidth = (comboBox.Items.Count > comboBox.MaxDropDownItems)
                  ? SystemInformation.VerticalScrollBarWidth
                  : 0;

                IEnumerable<string> itemsList = comboBox.Items.Cast<object>().Select(item => item.ToString());

                foreach (var s in itemsList)
                {
                  int newWidth = (int)g.MeasureString(s, font).Width + vertScrollBarWidth;

                  if (width < newWidth)
                  {
                width = newWidth;
                  }
                }

                comboBox.DropDownWidth = width;

                comboBox.Tag = true;
            }
Exemplo n.º 15
0
        /// <summary>
        /// Adjust the comboBox dropDownList width
        /// </summary>
        /// <param name="senderComboBox">the comboBox</param>
        private void AdjustComboBoxDropDownListWidth(ComboBox senderComboBox)
        {
            Graphics g = null;
            Font font = null;
            try
            {
                int width = senderComboBox.Width;
                g = senderComboBox.CreateGraphics();
                font = senderComboBox.Font;

                // checks if a scrollbar will be displayed.
                // if yes, then get its width to adjust the size of the drop down list.
                int vertScrollBarWidth =
                    (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
                    ? SystemInformation.VerticalScrollBarWidth : 0;

                int newWidth;
                foreach (object s in senderComboBox.Items)  //Loop through list items and check size of each items.
                {
                    if (s != null)
                    {
                        newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
                            + vertScrollBarWidth;
                        if (width < newWidth)
                        {
                            width = newWidth;   //set the width of the drop down list to the width of the largest item.
                        }
                    }
                }
                senderComboBox.DropDownWidth = width;
            }
            catch
            { }
            finally
            {
                if (g != null)
                {
                    g.Dispose();
                }
            }
        }