c#基于MaxLength从组合框中删除项目

时间:2015-03-27 14:30:15

标签: c# visual-studio-2013 combobox

所以我有一个组合框,我有它,以便根据其他标准编辑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:

1 个答案:

答案 0 :(得分:0)

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

我的comboBox名为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);

enter image description here 您的代码应如下所示:

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