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 ch5_3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        const int num = 5;
        int[] grades = new int[num];
        int count = 0; 
        private void Form1_Load(object sender, EventArgs e)
        {
            label1.Text += (count + 1).ToString();
            button1.Enabled = false;
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                grades[count ++] = Convert.ToInt32(textBox1.Text);
                if (count < num)
                {
                    textBox1.Text = "";
                    textBox1.Focus();
                    label1.Text = "Enter number : " + (count + 1).ToString();
                }
                else
                {
                    button1.Enabled = true;
                    MessageBox.Show("Please click Sort button");
                    count = 0; //if you want to enter new grades
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int min, minIndex, temp;
            //insert unsorted grades in listBox1
            foreach (int grade in grades)
                listBox1.Items.Add(grade);
            for (int i = 0; i < num - 1; i++)
            {
                min = grades[i];
                minIndex = i;
                for (int j = minIndex + 1; j < num; j++)
                    if (min > grades[j])
                    {
                        min = grades[j];
                        minIndex = j;
                    }
                    temp = grades[i];
                    grades[i] = min;
                    grades[minIndex] = temp;
            }
            foreach (int grade in grades)
                listBox2.Items.Add(grade);
        }
    }
}
