说明:C#中ComboBox控件在项过长时,会显示不全
例如:
    
    
   
运行之后,字体显示不完整
    
    
   
我们需要重写ComboBox控件
class CustomCombox : ComboBox {
        protected override void OnDropDown(EventArgs e) {
            base.OnDropDown(e);
            AdjustComboBoxDropDownListWidth();
        }
        private void AdjustComboBoxDropDownListWidth() {
            int vertScrollBarWidth = (this.Items.Count > this.MaxDropDownItems) ? SystemInformation.VerticalScrollBarWidth : 0;
            int maxWidth = this.DropDownWidth;
            foreach (var layouts in this.Items) {
                int measureTextWidth = TextRenderer.MeasureText(layouts.ToString(), this.Font).Width;
                maxWidth = maxWidth < measureTextWidth ? measureTextWidth : maxWidth;
            }
            this.DropDownWidth = maxWidth + vertScrollBarWidth;
        }
    }
同样的数据源,运行之后
    
    
   
 
版权声明:本文为nidexuanzhe原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。