2015-10-20 55 views
0

所以我有一個列表框中的高中名單。現在大約有15所學校,但我會增加更多的學校。當我添加更多的學校時,如果有其他陳述,我不想添加所有這些。我正在考慮使用陳述,但我仍然不真正理解陳述的概念。 我嘗試過使用for語句,但是我再次不理解它們。當我調試時,它會顯示列表框中的項目數量,然後列出所有項目。我只希望它顯示一個名字並告訴我它的排名。代碼如下。Listbox自動搜索

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Search_the_list 
{ 
public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     /*if 
      (listBox1.SelectedIndex == 0) 
      MessageBox.Show("Granda High School has 82 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 1) 
      MessageBox.Show("Livermore High School has 84 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 2) 
      MessageBox.Show("Dublin High School has 150 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 3) 
      MessageBox.Show("Galt High School has 76 Faculty Members"); 

     else 
     if 
      (listBox1.SelectedIndex == 4) 
      MessageBox.Show("Foothill High School has 73 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 5) 
      MessageBox.Show("Amador Valley High School has 83 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 6) 
      MessageBox.Show("California High School has 92 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 7) 
      MessageBox.Show("Dougherty High School has 76 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 8) 
      MessageBox.Show("Mission San Jose High School has 67 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 9) 
      MessageBox.Show("Monte Vista High School has 88 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 10) 
      MessageBox.Show("San Ramon High School has 84 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 11) 
      MessageBox.Show("Evergreen Valley High School has 124 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 12) 
      MessageBox.Show("Irvington High School has 84 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 13) 
      MessageBox.Show("Lincoln High School has 84 Faculty Members"); 
     else 
     if 
      (listBox1.SelectedIndex == 14) 
      MessageBox.Show("American High School has 102 Faculty Members"); 
     else 
      MessageBox.Show("Please select a valid school");*/ 
     int count = listBox1.Items.Count; 


     /* MessageBox.Show("" + count); 
     string name = listBox1.SelectedItem.ToString(); 
     MessageBox.Show("" + count); 
     MessageBox.Show("" + name);*/ 
     MessageBox.Show(count.ToString()); 
     for (int i = 0; i < count; i++) 
     { 
      MessageBox.Show("" + i); 
     } 
    } 
} 

回答

0

這個問題和解決方案(S)可能是非常有幫助的: WPF: C# How to get selected value of Listbox?

在Button1_Click的,你可以這樣做:

var school = listBox1.SelectedItem as SchoolInfo; 


if (school != null) 
{ 

    MessageBox.Show(school.Name + " has " + school.NumFaculty + " faculty members."); 

} 

此,如果你的學校信息類,使被你的清單是這樣的:

public class SchoolInfo 
{ 

    public string Name { get; set; } 

    public int NumFaculty { get; set; } 

}