2016-09-29 61 views
0

嗨,我想弄清楚如何比較組合框中每個單獨項目的字符長度。 我嘗試了一些東西,但沒有任何工作。如何比較組合框中每個項目的字長?

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

但這個foreach給我一個錯誤。

comboBox1.Text.Length 

這確實給了我所選擇的項目的長度,但沒有比較所有這些。 我會讚賞你的幫助!

+2

它應該是'comboBox1 .Items'。 – Berkay

回答

3

您不是遍歷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(); 
0

它應該是這樣的:

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

答案取決於一個幾個因素。 string或其他一些類別的項目是? ypu手動填寫您的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; 

如果重寫ToString()方法MyClass你可以這樣做的:

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(); 

。 ..或者你可以使用的:

 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(); 
相關問題