本文整理汇总了C#中System.Windows.Forms.ComboBox.CreateGraphics方法的典型用法代码示例。如果您正苦于以下问题:C# ComboBox.CreateGraphics方法的具体用法?C# ComboBox.CreateGraphics怎么用?C# ComboBox.CreateGraphics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Forms.ComboBox
的用法示例。
在下文中一共展示了ComboBox.CreateGraphics方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SetComboBoxWidth
/// <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;
}
示例2: ResizeComboBoxDropDownWidth
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);
}
示例3: CalculateComboBoxDropdownWidth
// 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;
}
示例4: AutoSizeDropDown
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;
}
示例5: CalculateDropDownWidth
/// <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;
}
示例6: SetComboBoxDropDownWidth
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;
}
示例7: InitializeTextEncodingComboBox
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;
}
示例8: InitializeSubtitleFormatComboBox
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();
}
示例9: GetIdealDropDownWidth
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;
}
示例10: ResizeDropDownForLongestEntry
/// <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;
}
}
示例11: getComboBoxWith
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;
}
示例12: DropDown
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;
}
示例13: AdjustComboBoxDropDownListWidth
/// <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();
}
}
}