Files
2022-11-03 17:48:04 -05:00

55 lines
1.9 KiB
C#
Executable File

namespace RandomNumberFileWriterTwo_CalebFontenot
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void openButton_Click(object sender, EventArgs e)
{
// Define variables
int randomNumberSum = 0, randomNumberCount = 0, inputNumber;
// Create StreamReader
StreamReader inputFile = null;
// Summon the OpenFile Dialog for selecting the text file to read from.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Open the user selected file.
inputFile = File.OpenText(openFileDialog1.FileName);
// Clear the list box, just in case this isn't the first time this fuction has run.
numberOutputListBox.Items.Clear();
// Read from the text file
while (!inputFile.EndOfStream)
{
try
{
inputNumber = int.Parse(inputFile.ReadLine());
}
catch
{
MessageBox.Show("Error interpreting contents of text file. Are you sure you selected the right file?");
break;
}
numberOutputListBox.Items.Add(inputNumber);
randomNumberSum += inputNumber;
randomNumberCount++;
}
// Write randomNumberSum and randomNumberCount to the appropriate labels.
numberSumLabel.Text = randomNumberSum.ToString();
numberCountLabel.Text = randomNumberCount.ToString();
}
}
}
}