lab6_2
This commit is contained in:
74
lab6_1/lab6_3_CalebFontenot/Form1.cs
Executable file
74
lab6_1/lab6_3_CalebFontenot/Form1.cs
Executable file
@@ -0,0 +1,74 @@
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user