North America


// Gaddis Code from text with slight mods (Wyatt)
//
using System;
using System.Windows.Forms;
using System.IO;

namespace North_America
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        // button click calls two user-defined methods
        private void getCountriesButton_Click(object sender, EventArgs e)
        {
            string filename;    // To hold the filename

            // Get the filename from the user.
            GetFileName(out filename);
            
            // Get the countries from the file.
            GetCountries(filename);
        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            // Close the form.
            this.Close();
        }
        
        // The GetFileName method gets a filename from the user
        // and assigns it to the variable passed as an argument.
        private void GetFileName(out string selectedFile)
        {
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                selectedFile = openFile.FileName;
            }
            else
            {
                selectedFile = "";
            }
        }

        // The GetCountries method accpets a filename as an argument. It opens the 
        // specified file and displays its contents in the countriesListBox control.
        private void GetCountries(string filename)
        {
            try
            {
                // Declare a variable to hold a country name.
                string countryName;
                
                // Declare a StreamReader variable.
                StreamReader inputFile;

                // Open the file and get a StreamReader object.
                inputFile = File.OpenText(filename);

                // Clear anything currently in the ListBox.
                countriesListBox.Items.Clear();

                // Read the file's contents.
                while (!inputFile.EndOfStream)
                {
                    // Get a country name.
                    countryName = inputFile.ReadLine();

                    // Add the country name to the ListBox.
                    countriesListBox.Items.Add(countryName);
                }

                // Close the file.
                inputFile.Close();
            }
            catch (Exception ex)
            {
                // Display an error message.
                MessageBox.Show(ex.Message);
            }
        }


    }
}