当前位置:编程学习 > C#/ASP.NET >>

VB .NET 2008的ComboBox控件如何实现自动根据列表的内容自动调节列表宽度

VB .NET 2008的ComboBox控件如何实现自动根据列表的内容自动调节列表宽度?
万谢! --------------------编程问答-------------------- 一段C#代码,应该好转成VB.net的
private void AdjustComboBoxDropDownListWidth()
         {
            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();
            }
        }
--------------------编程问答-------------------- 自己重写个控件就可以了 按照combox的里面的内容长度来决定显示长度
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,