C# c根据MaxLength从组合框中删除项目

C# c根据MaxLength从组合框中删除项目,c#,visual-studio-2013,combobox,C#,Visual Studio 2013,Combobox,因此,我有一个组合框,我有它,以便根据其他标准,编辑MaxLength。问题是MaxLength属性仅适用于类型化输入,而不适用于组合框中的项。那么,有没有一种方法可以根据项目的长度从组合框中删除项目,并在长度再次增加时重新添加?这是我的密码: private void titleText_TextChanged(object sender, EventArgs e) { int colourNum; int textType = 1;

因此,我有一个组合框,我有它,以便根据其他标准,编辑MaxLength。问题是MaxLength属性仅适用于类型化输入,而不适用于组合框中的项。那么,有没有一种方法可以根据项目的长度从组合框中删除项目,并在长度再次增加时重新添加?这是我的密码:

private void titleText_TextChanged(object sender, EventArgs e)
    {
        int colourNum;
        int textType = 1;

        if (titleTextType.Text == "Material")
            textType = 1;
        else if (titleTextType.Text == "Material Flipped")
            textType = 2;
        else if (titleTextType.Text == "Normal")
            textType = 3;
        else if (titleTextType.Text == "3D")
            textType = 4;
        else textType = 0;


        if (titleTextColour.Text == "Red")
            colourNum = 49;
        else if (titleTextColour.Text == "Green")
            colourNum = 50;
        else if (titleTextColour.Text == "Yellow")
            colourNum = 51;
        else if (titleTextColour.Text == "Blue")
            colourNum = 52;
        else if (titleTextColour.Text == "Cyan")
            colourNum = 53;
        else if (titleTextColour.Text == "Pink")
            colourNum = 54;
        else if (titleTextColour.Text == "White")
            colourNum = 55;
        else if (titleTextColour.Text == "Black")
            colourNum = 48;
        else if (titleTextColour.Text == "Yale Blue")
            colourNum = 59;
        else if (titleTextColour.Text == "Light Yellow")
            colourNum = 58;
        else colourNum = 0;

        byte[] colourArray = new byte[2]
                {
                    (byte) 94,
                    (byte) colourNum
                };
        byte[] prefixArray1 = new byte[5]
                {
                    (byte) 94,
                    (byte) textType,
                    (byte) 0x3D,//125decmax
                    (byte) 0x3D,//125decmax
                    (byte) titleText.Text.Length
                };


        if (textType == 3 && colourNum == 0)
        {
            titleText.MaxLength = 23;
        }
        else if (textType == 3 && colourNum != 0)
        {
            titleText.MaxLength = 21;
        }
        else if (textType == 1 && colourNum == 0 || textType == 2 && colourNum == 0)
        {
            titleText.MaxLength = 18;
        }
        else if (textType == 1 && colourNum != 0 || textType == 2 && colourNum != 0)
        {
            titleText.MaxLength = 16;
        }
        else if (textType == 4)
        {
            titleText.MaxLength = 3;
        }
    }

提前感谢c:

在这里,将更容易在这里演示代码

我的组合框名为comboBox1

此测试代码添加Test1-Test20,然后根据长度删除它们:

for (int x = comboBox1.Items.Count-1; x >= 0; x--)
    if (comboBox1.Items[x].ToString().Length > comboBox1.MaxLength)
        comboBox1.Items.RemoveAt(x);
您的代码应该如下所示:

for (int x = titleText.Items.Count-1; x >= 0; x--)
    if (titleText.Items[x].ToString().Length > titleText.MaxLength)
        titleText.Items.RemoveAt(x);

使用on字符串怎么样?您是否试图限制显示的项目数或按其长度过滤项目?forint x=titleText.items.Count;x>0;x-{iftitleText.Items[x].Text.Length>titleText.MaxLength titleText.Items.RemoveAtx;}或类似的内容-我没有打开IDE。使用lambda表达式比较长度可能会更快-我错过了c@user1274820谢谢,我可以试一试。@user1274820,我不能将Text.Length属性用于titleText的任何项目。抱歉,我不太擅长编程:c。如果我可以,我总是很乐意帮助c: