ASDV-C-Sharp/lab6_1/lab6_3_CalebFontenot/Form1.cs
2022-11-15 14:22:17 -06:00

74 lines
2.3 KiB
C#
Executable File

namespace lab6_3_CalebFontenot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitButton_Click(object sender, EventArgs e)
{
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 accepts 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);
}
}
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);
}
}
}