列表框自动搜索

时间:2015-10-20 17:52:02

标签: c# listbox

所以我列出了一个列表框中的高中名单。现在大约有15所学校,但我要增加更多学校。当我添加更多学校时,我真的不想添加所有那些if else if语句。我正在考虑使用for语句,但我仍然不太了解for语句的概念。 我尝试使用for语句,但同样,我不理解它们。当我调试时,它会显示列表框中的项目数,然后列出所有项目。我只希望它显示一个名字并告诉我它的排名。代码如下。

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

1 个答案:

答案 0 :(得分:0)

这个问题和解决方案可能非常有用: 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; }

}