Tuesday, April 22, 2014

Zau_TheListCollection_Lab



namespace Zau_TheListCollection_Lab
{

    public partial class Form1 : Form
    {
        List<string> students;

        public Form1()
        {
            InitializeComponent();
        }
        //method to display the list
        private void DisplayList()
        {
            listBox1.Items.Clear();
            foreach (string n in students)
            {
                listBox1.Items.Add(n);
            }
        }
        //method to display Count and Capacity of list numbers
        private void DisplayCountCapacity()
        {
            txtCountNCapacity.Text = String.Format(
                " Count: {0} \n Capacity: {1}", students.Count, students.Capacity);
        }

        private void btnDisplayList_Click(object sender, EventArgs e)
        {
            DisplayList();
            DisplayCountCapacity();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            students = new List<string> { "Mark", "Joe", "Ann", "Sam", "Joshua", "Rose", "Katy", "Maggie", "Moses", "Nathaniel" };
            DisplayList();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
         
            string students = txtAddRemove.Text.Trim();
            //check if the textbox is not empty
            if (students != String.Empty)
            {
                //add it to the listbox
                listBox1.Items.Add(students);
            }
            txtAddRemove.Clear();
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            listBox1.SelectionMode = SelectionMode.One;

            for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--)
            {
                listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
            }
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            int index = int.Parse(txtIndex.Text);
            string stdname = txtName.Text;
            if (listBox1.Items.Count == 0)
                listBox1.Items.Add(stdname);
            if (index >= 0 && index <= listBox1.Items.Count)
            {

                listBox1.Items.Insert(index, stdname);
                txtName.Clear();
            }
            else
                MessageBox.Show("Invalid Index");
            txtName.Clear();
            txtIndex.Clear();
        }

        private void btnCheck_Click(object sender, EventArgs e)
        {
            string name = txtCheck.Text;
            if (listBox1.Items.Contains(name))
            {
                MessageBox.Show("Name is in the list");
            }
            else
            {
                MessageBox.Show("Sorry, Name doesn't exist in the list",
                                 "Out of range exception",
                                 MessageBoxButtons.OK,
                                 MessageBoxIcon.Error);
            }
        }
    }
}
//new project: 
///create a list of strings
///Add method to display the list to a listbox
///Add method to display count and capacity to a richtextbox
///in form1-load add about 10 names to the list, then
///display the list as well as count and capacity
///Include Gui to allow you to add a name to the list
///Include Gui to allow you to remove from the list
///a name that is selected in the listbox (set to one selection)
///
/// Include gui to enter a name and check whether the name is
/// contained in the list
/// Include gui to insert a name at a given index.

No comments:

Post a Comment