0

So I have a list of high schools in a listbox. there are about 15 schools right now, but I'm going to add more schools. When I add more schools, I don't really want to add all those if else if statements. I was thinking about using for statements, but I still don't really understand the concept of for statements. I tried using a for statement, but again, I don't understand them. When I debug, it shows me the number of items in the listbox, then lists all of them. I only want it to display one name and tell me its ranking. the code is below.

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);
        }
    }
}
Manos Nikolaidis
  • 21,608
  • 12
  • 74
  • 82

1 Answers1

0

This question and solution(s) may be very helpful: WPF: C# How to get selected value of Listbox?

In the button1_Click you could do something like:

var school = listBox1.SelectedItem as SchoolInfo; 


if (school != null)
{

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

}

This is if your School Info class that makes up your list is something like this:

public class SchoolInfo
{

   public string Name { get; set; }

   public int NumFaculty { get; set; }

}
Community
  • 1
  • 1
Smurfie
  • 124
  • 5