[英]Auto-width of ComboBox's content
有人知道將ComboBox
的內容寬度設置為自動調整大小的方法嗎
我指的不是ComboBox
本身,而是打開的內容。
你不能直接使用它。
做個小把戲
首先遍歷組合框的所有項目,通過將文本分配給標簽來檢查每個項目的寬度。 然后,每次檢查寬度,如果當前項目的寬度大於以前的項目,則更改最大寬度。
int DropDownWidth(ComboBox myCombo)
{
int maxWidth = 0;
int temp = 0;
Label label1 = new Label();
foreach (var obj in myCombo.Items)
{
label1.Text = obj.ToString();
temp = label1.PreferredWidth;
if (temp > maxWidth)
{
maxWidth = temp;
}
}
label1.Dispose();
return maxWidth;
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownWidth = DropDownWidth(comboBox1);
}
或者
根據stakx 的建議,您可以使用TextRenderer
類
int DropDownWidth(ComboBox myCombo)
{
int maxWidth = 0, temp = 0;
foreach (var obj in myCombo.Items)
{
temp = TextRenderer.MeasureText(obj.ToString(), myCombo.Font).Width;
if (temp > maxWidth)
{
maxWidth = temp;
}
}
return maxWidth;
}
obj.ToString() 對我不起作用,我建議使用 myCombo.GetItemText(obj)。 這對我有用:
private int DropDownWidth(ComboBox myCombo)
{
int maxWidth = 0, temp = 0;
foreach (var obj in myCombo.Items)
{
temp = TextRenderer.MeasureText(myCombo.GetItemText(obj), myCombo.Font).Width;
if (temp > maxWidth)
{
maxWidth = temp;
}
}
return maxWidth + SystemInformation.VerticalScrollBarWidth;
}
這是非常優雅的解決方案。 只需將您的組合框訂閱到此事件處理程序:
private void AdjustWidthComboBox_DropDown(object sender, EventArgs e)
{
var senderComboBox = (ComboBox)sender;
int width = senderComboBox.DropDownWidth;
Graphics g = senderComboBox.CreateGraphics();
Font font = senderComboBox.Font;
int vertScrollBarWidth = (senderComboBox.Items.Count > senderComboBox.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
var itemsList = senderComboBox.Items.Cast<object>().Select(item => item.ToString());
foreach (string s in itemsList)
{
int newWidth = (int)g.MeasureString(s, font).Width + vertScrollBarWidth;
if (width < newWidth)
{
width = newWidth;
}
}
senderComboBox.DropDownWidth = width;
}
此代碼取自 codeproject: Adjust combo box drop down list width to最長字符串寬度。 但是我已經修改了它以使用填充了任何數據(不僅是字符串)的組合框。
與 Javed Akram 的第二個建議中的代碼大致相同,但添加了垂直滾動條的寬度:
int setWidth_comboBox(ComboBox cb)
{
int maxWidth = 0, temp = 0;
foreach (string s in cb.Items)
{
temp = TextRenderer.MeasureText(s, cb.Font).Width;
if (temp > maxWidth)
{
maxWidth = temp;
}
}
return maxWidth + SystemInformation.VerticalScrollBarWidth;
}
使用這樣的代碼(在帶有名稱為 myComboBox 的組合框的表單上):
myComboBox.Width = setWidth_comboBox(myComboBox);
投票支持下面的 algreat 答案。
我只是修改了 algreat 的答案,代碼調整了整個控件的大小。
我只是將其添加為評論,但無法在評論中添加格式化代碼。
private void combo_DropDown(object sender, EventArgs e)
{
//http://www.codeproject.com/Articles/5801/Adjust-combo-box-drop-down-list-width-to-longest-s
ComboBox senderComboBox = (ComboBox)sender;
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 (string s in ((ComboBox)sender).Items)
{
newWidth = (int)g.MeasureString(s, font).Width
+ vertScrollBarWidth;
if (width < newWidth)
{
width = newWidth;
}
if (senderComboBox.Width < newWidth)
{
senderComboBox.Width = newWidth+ SystemInformation.VerticalScrollBarWidth;
}
}
senderComboBox.DropDownWidth = width;
}
請在下面查看我的解決方案:
private int AutoSizeDropDownWidth(ComboBox comboBox)
{
var width = cmboxUnit.DropDownWidth;
var g = cmboxUnit.CreateGraphics();
var font = cmboxUnit.Font;
var verticalScrollBarWidth = cmboxUnit.Items.Count > cmboxUnit.MaxDropDownItems
? SystemInformation.VerticalScrollBarWidth : 0;
var itemsList = cmboxUnit.Items.Cast<object>().Select(item => item);
foreach (DataRowView dr in itemsList)
{
int newWidth = (int)g.MeasureString(dr["Name"].ToString(), font).Width + verticalScrollBarWidth;
if (width < newWidth)
{
width = newWidth;
}
}
return width;
}
這是一個老問題,但我剛遇到它並結合了我的解決方案的幾個答案。 我喜歡接受的答案的簡單性,但想要一些可以與組合框中的任何對象類型一起使用的東西。 我還想通過擴展方法來使用該方法。
public static int AutoDropDownWidth(this ComboBox myCombo)
{
return AutoDropDownWidth<object>(myCombo, o => o.ToString());
}
public static int AutoDropDownWidth<T>(this ComboBox myCombo, Func<T, string> description)
{
int maxWidth = 1;
int temp = 1;
int vertScrollBarWidth = (myCombo.Items.Count > myCombo.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
foreach (T obj in myCombo.Items)
{
if (obj is T)
{
T t = (T)obj;
temp = TextRenderer.MeasureText(description(t), myCombo.Font).Width;
if (temp > maxWidth)
{
maxWidth = temp;
}
}
}
return maxWidth + vertScrollBarWidth;
}
這樣,如果我的班級是:
public class Person
{
public string FullName {get;set;}
}
我可以像這樣自動調整組合框下拉寬度:
cbPeople.DropDownWidth = cbPeople.AutoDropDownWidth<Person>(p => p.FullName);
舊但經典,希望工作夠快
private int GetDropDownWidth(ComboBox combo)
{
object[] items = new object[combo.Items.Count];
combo.Items.CopyTo(items, 0);
return items.Select(obj => TextRenderer.MeasureText(combo.GetItemText(obj), combo.Font).Width).Max();
}
TComboBox.ItemWidth 是您要尋找的屬性。 它具有您想要的行為,無需任何編碼。 只需在設計時或以編程方式將其設置為大於 Width 的值,當用戶拉下該框時,他們將獲得更廣泛的選擇列表。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.