C# 列表项下拉窗口宽度自适应

/// <summary>
        /// 列表项下拉窗口宽度自适应
        /// </summary>
        /// <param name="comboBox"></param>
        private void ComboBoxAutoDropDownListWidth(ComboBox comboBox)
        {
            int maxWidth = 0, temp = 0;
            foreach (var obj in comboBox.Items)
            {
                temp = TextRenderer.MeasureText(comboBox.GetItemText(obj), comboBox.Font).Width;
                if (temp > maxWidth)
                {
                    maxWidth = temp;
                }
            }
            comboBox.DropDownWidth= maxWidth + SystemInformation.VerticalScrollBarWidth+2;
        }

https://www.itranslater.com/qa/details/2583418447979873280

posted @ 2021-11-08 17:56  LuoCore  阅读(299)  评论(0编辑  收藏  举报