I clearly have no idea how to do this and not getting a whole lot of help from teachers. I have instructions to:
Part A: Read File into Array Read a text file of strings into a string array. Create the array bigger than the file. Count how many strings are in the array.
Part B: Enter String into Array Input a string from a text box. Assign the string into the first empty array element. Update the count of strings in the array.
Part C: Display Array in List Box Display the strings from the array in a list box. Don’t display unused array elements (more than the count).
Write Array to File Write the strings from the array to a text file
I'm not sure how to write any of this and the furthest I have gotten is here which is wrong and messy. Any help would be appreciated.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string ReadStringFile(
string fileName, //name of file to read
string[] contents, //array to store strings
int max) //size of array
{
//Variables
int count; //count of numbers
//Catch exceptions
try
{
StreamReader stringFile = File.OpenText(fileName);
//Read strings from file
count = 0;
while (!stringFile.EndOfStream && count < max)
{
contents[count] = stringFile.ReadLine();
count++;
}
//Close file
stringFile.Close();
//Return to user
return count.ToString();
}
catch (Exception ex)
{
MessageBox.Show("Sorry, error reading file: " + ex.Message);
return "";
}
}
private void DisplayStrings(string[] contents, int count)
{
//Display strings in list box
for (int i = 0; i < count; i++)
{
stringListBox.Items.Add(contents[i]);
}
}
private void ProcessButton_Click(object sender, EventArgs e)
{
//Variables
string fileName = "strings.txt";
const int SIZE = 5;
string[] contents = new string[SIZE];
int count;
ReadStringFile(fileName, contents, SIZE);
DisplayStrings(contents, count);
}
}




