我正在尝试获取 DropDownList 的宽度,并且它的末尾不断出现“px”。我所追求的最终结果是确定 DropDownList 中所有项目的宽度,以便我可以以编程方式设置宽度。
这是我所拥有的:
    public static int FindTheWidth(object sender)
    {
        DropDownList senderComboBox2 = (DropDownList)sender;
        //            int width = senderComboBox.Width;
        var s = senderComboBox2.Width;
        var CBW = s.SubString(0, s.IndexOf("p"));
        int width2 = Convert.ToInt32(CBW);
        int vertScrollBarWidth = 2;
        int newWidth;
        foreach (string s in ((DropDownList)sender).Items)
        {
            newWidth = width2 + vertScrollBarWidth;
            if (width2 < newWidth)
            {
                width2 = newWidth;
            }
        }
        senderComboBox2.Width = width2;
        return width2;
    }
有任何想法吗?