C# 组合框的宽度也一样,单击下拉列表后,看到宽度发生变化时,它看起来很奇怪。因为我将组合框动态添加到FlowLayoutPanel中,所以我订阅了ParentChanged事件,它看起来好多了。捕捉得不错myCombo.GetItemText(obj)肯定比其
C# 组合框的宽度也一样,单击下拉列表后,看到宽度发生变化时,它看起来很奇怪。因为我将组合框动态添加到FlowLayoutPanel中,所以我订阅了ParentChanged事件,它看起来好多了。捕捉得不错myCombo.GetItemText(obj)肯定比其,c#,.net,winforms,combobox,C#,.net,Winforms,Combobox,组合框的宽度也一样,单击下拉列表后,看到宽度发生变化时,它看起来很奇怪。因为我将组合框动态添加到FlowLayoutPanel中,所以我订阅了ParentChanged事件,它看起来好多了。捕捉得不错myCombo.GetItemText(obj)肯定比其他答案更正确obj.ToString()可以不同于组合框的DisplayMember属性。首先,问题是要求自动调整大小,这与显式设置宽度正好相反。但是第二,你在这里的意思还不清楚。什么是密码箱?您谈论的是什么ItemWidth属性?什么也没找到
组合框的宽度也一样,单击下拉列表后,看到宽度发生变化时,它看起来很奇怪。因为我将组合框动态添加到FlowLayoutPanel中,所以我订阅了ParentChanged事件,它看起来好多了。捕捉得不错
myCombo.GetItemText(obj)
肯定比其他答案更正确obj.ToString()
可以不同于组合框的DisplayMember
属性。首先,问题是要求自动调整大小,这与显式设置宽度正好相反。但是第二,你在这里的意思还不清楚。什么是密码箱?您谈论的是什么ItemWidth
属性?什么也没找到。
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);
}
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;
}
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;
}
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.Width = setWidth_comboBox(myComboBox);
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;
}
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 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 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();
}
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;
}