如何比较组合框中每个项目的单词长度

本文关键字:项目 单词长 何比较 比较 组合 | 更新日期: 2023-09-27 18:09:33

嗨,我想弄清楚如何比较组合框中每个单独项目的字符长度。我试了一些方法,但都不起作用。

string longestName = "";
foreach (string possibleDate in comboBox1)

但是这个foreach给了我一个错误。

comboBox1.Text.Length

给我选择的项目的长度,但不比较所有的。谢谢你的帮助!

如何比较组合框中每个项目的单词长度

您没有遍历ComboBox的项目列表。

试试这样写:

string longestName = "";
foreach (string possibleDate in comboBox1.Items)
{
    int stringLength = possibleDate.Length;
    if(stringLength > longestName.Length)
        longestName = possibleDate;
}

或者你可以跳过它,使用LINQ:

var longestName = comboBox1.Items.Cast<string>().OrderByDescending(item => item.Length).First();

应该是这样的:

  string longestName = "";
    foreach (string possibleDate in comboBox1.Items){
       if(longestName.Length < possibleDate.Length){
          longestName = possibleDate;
       }
    }

答案取决于几个因素。这些物品是string还是其他种类的?您是手动填充ComboBox还是绑定到DataSource ?

手动填充/类型为string

        string longestName = "";
        comboBox1.Items.Add("aaa ddd ddd");
        comboBox1.Items.Add("aaa");
        comboBox1.Items.Add("aaa ff");
        comboBox1.Items.Add("aaa x");
        foreach (string possibleDate in comboBox1.Items)
        {
            int stringLength = possibleDate.Length;
            if (stringLength > longestName.Length)
                longestName = possibleDate;
        }
        Console.WriteLine(longestName);
        //or:
        string longest = comboBox1.Items.Cast<string>().OrderByDescending(a => a.Length).FirstOrDefault();

手动填充/复杂类型

    class MyClass
    {
        public string MyProperty { get; set; }
    }
        comboBox1.Items.Add(new MyClass { MyProperty = "aaa ddd ddd" });
        comboBox1.Items.Add(new MyClass { MyProperty = "aaa" });
        comboBox1.Items.Add(new MyClass { MyProperty = "aaa ff" });
        comboBox1.Items.Add(new MyClass { MyProperty = "aaa x" });
        foreach (MyClass possibleDate in comboBox1.Items)
        {
            int stringLength = possibleDate.MyProperty.Length;
            if (stringLength > longestName.Length)
                longestName = possibleDate.MyProperty;
        }
        Console.WriteLine(longestName);
        //or
        string longest = comboBox1.Items.Cast<MyClass>().OrderByDescending(a => a.MyProperty.Length).FirstOrDefault().MyProperty;

如果您为MyClass重写ToString()方法,您可以这样做:

    class MyClass
    {
        public string MyProperty { get; set; }
        public override string ToString()
        {
            return MyProperty;
        }
    }
    string longest = comboBox1.Items.Cast<MyClass>().OrderByDescending(a => a.ToString()).FirstOrDefault().ToString();

…或者你可以使用ComboBox:

中的GetItemText()
        foreach (MyClass possibleDate in comboBox1.Items)
        {
            int stringLength = comboBox1.GetItemText(possibleDate).Length;
            if (stringLength > longestName.Length)
                longestName = comboBox1.GetItemText(possibleDate);
        }

如果你的ComboBox是绑定的,你不需要重写ToString()方法来使用GetItemText(),这样它就独立于项目的类型:

    class MyClass
    {
        public decimal MyProperty { get; set; }
    }
         BindingList<MyClass> source = new BindingList<MyClass>
        {
            new MyClass { MyProperty = 1.0001m},
            new MyClass { MyProperty = 100001.5555m},
            new MyClass { MyProperty = 4m},
            new MyClass { MyProperty = 300.5m }
        };
        comboBox1.DataSource = source;
        comboBox1.DisplayMember = "MyProperty";
        foreach (object possibleDate in comboBox1.Items)
        {
            int stringLength = comboBox1.GetItemText(possibleDate).Length;
            if (stringLength > longestName.Length)
                longestName = comboBox1.GetItemText(possibleDate);
        }
        Console.WriteLine(longestName);
        //or
        string longest = comboBox1.Items.Cast<object>().Select(a => comboBox1.GetItemText(a)).OrderByDescending(a => a.Length).FirstOrDefault();