VB .NET 2008的ComboBox控件如何实现自动根据列表的内容自动调节列表宽度
VB .NET 2008的ComboBox控件如何实现自动根据列表的内容自动调节列表宽度?万谢! --------------------编程问答-------------------- 一段C#代码,应该好转成VB.net的
private void AdjustComboBoxDropDownListWidth()--------------------编程问答-------------------- 自己重写个控件就可以了 按照combox的里面的内容长度来决定显示长度
{
Graphics g = null;
Font font = null;
try
{
int width = this.Width;
g = this.CreateGraphics();
font = this.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 =
(this.Items.Count > this.MaxDropDownItems)
? SystemInformation.VerticalScrollBarWidth : 0;
int newWidth;
foreach (object s in this.Items) //Loop through list items and check size of each items.
{
if (s != null)
{
//newWidth = (int)g.MeasureString(s.ToString().Trim(), font).Width
newWidth = (int)g.MeasureString(((ComboBoxData)s).StrText, font).Width
+ vertScrollBarWidth;
if (width < newWidth)
width = newWidth; //set the width of the drop down list to the width of the largest item.
}
}
this.DropDownWidth = width;
}
catch
{ }
finally
{
if (g != null)
g.Dispose();
}
}
补充:.NET技术 , VB.NET