C# 如何比较组合框中每个项目的字长?

C# 如何比较组合框中每个项目的字长?,c#,combobox,compare,C#,Combobox,Compare,嗨,我想知道如何比较组合框中每个单独项目的字符长度。 我试了几件事,但都没用 string longestName = ""; foreach (string possibleDate in comboBox1) 但是这个foreach给了我一个错误 comboBox1.Text.Length 这会给出所选项目的长度,但不会比较所有项目。 我将感谢你的帮助 您没有遍历组合框的项目列表 试着这样做: string longestName = ""; foreach (string possib

嗨,我想知道如何比较组合框中每个单独项目的字符长度。 我试了几件事,但都没用

string longestName = "";
foreach (string possibleDate in comboBox1)
但是这个foreach给了我一个错误

comboBox1.Text.Length
这会给出所选项目的长度,但不会比较所有项目。
我将感谢你的帮助

您没有遍历组合框的项目列表

试着这样做:

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();
var longestName=comboBox1.Items.Cast().OrderByDescending(item=>item.Length.First();

您没有遍历组合框的项目列表

试着这样做:

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();
var longestName=comboBox1.Items.Cast().OrderByDescending(item=>item.Length.First();

应该是这样的:

  string longestName = "";
    foreach (string possibleDate in comboBox1.Items){
       if(longestName.Length < possibleDate.Length){
          longestName = possibleDate;
       }
    }
string longestName=”“;
foreach(comboBox1.Items中的字符串可能值){
if(longestName.Length
应该是这样的:

  string longestName = "";
    foreach (string possibleDate in comboBox1.Items){
       if(longestName.Length < possibleDate.Length){
          longestName = possibleDate;
       }
    }
string longestName=”“;
foreach(comboBox1.Items中的字符串可能值){
if(longestName.Length
答案取决于几个因素。是
string
类型的项目还是其他类别的项目?ypu是手动填充您的
组合框
,还是绑定到
数据源

手动填充的/类型为字符串的

        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();
…或者您可以使用组合框的
GetItemText()

        foreach (MyClass possibleDate in comboBox1.Items)
        {

            int stringLength = comboBox1.GetItemText(possibleDate).Length;
            if (stringLength > longestName.Length)
                longestName = comboBox1.GetItemText(possibleDate);
        }
如果您的
组合框
已绑定,则不必重写
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();
class-MyClass
{
公共十进制MyProperty{get;set;}
}
BindingList源=新的BindingList
{
新的MyClass{MyProperty=1.0001m},
新MyClass{MyProperty=100001.5555m},
新的MyClass{MyProperty=4m},
新MyClass{MyProperty=300.5m}
};
comboBox1.DataSource=源;
comboBox1.DisplayMember=“MyProperty”;
foreach(comboBox1.Items中的对象可能日期)
{
int-stringLength=comboBox1.GetItemText(possibleDate).Length;
如果(stringLength>longestName.Length)
longestName=comboBox1.GetItemText(可能的日期);
}
Console.WriteLine(最长名称);
//或
string longest=comboBox1.Items.Cast().Select(a=>comboBox1.GetItemText(a)).OrderByDescending(a=>a.Length.FirstOrDefault();

答案取决于几个因素。是
string
类型的项目还是其他类别的项目?ypu是手动填充您的
组合框
,还是绑定到
数据源

手动填充的/类型为字符串的

        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();
…或者您可以使用组合框的
GetItemText()

        foreach (MyClass possibleDate in comboBox1.Items)
        {

            int stringLength = comboBox1.GetItemText(possibleDate).Length;
            if (stringLength > longestName.Length)
                longestName = comboBox1.GetItemText(possibleDate);
        }
如果您的
组合框
已绑定,则不必重写
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();
class-MyClass
{
公共十进制MyProperty{get;set;}
}
BindingList源=新的BindingList
{
新的MyClass{MyProperty=1.0001m},
新MyClass{MyProperty=100001.5555m},
新的MyClass{MyProperty=4m},
新MyClass{MyProperty=300.5m}
};
comboBox1.DataSource=源;
comboBox1.DisplayMember=“MyProperty”;
foreach(comboBox1.Items中的对象可能日期)
{
int-stringLength=comboBox1.GetItemText(possibleDate).Length;
如果(stringLength>longestName.Length)
longestName=comboBox1.GetItemText(可能的日期);
}
Console.WriteLine(最长名称);
//或
string longest=comboBox1.Items.Cast().Select(a=>comboBox1.GetItemText(a)).OrderByDescending(a=>a.Length.FirstOrDefault();

它应该是
comboBox1.Items
。它应该是
comboBox1.Items