//Make a List to hold items from a file. Read the file and assign each item to my new List<> of strings. //This is taken from my Home Sprinkler contoller program. This allows me to let the user change the text //properties of the controls at run time. They can change the name of the sprinkler stations to suite their needs. //NOTE All my Messageboxes are handled in another Class using a method called NotifyUser. I simply pass in the //message I want and the method creates the Messagebox with buttons and Icon and caption. internal static List StationDescrip = new List(); internal static void ReadSettingsFile() { //---read data file and assign to items in List<> defined in General Class try { using (StreamReader sr = new StreamReader(myFileName)) { //---read all 8 items in the file and assign it to dataFromFile variable string dataFromFile; for (int x = 0; x < 8; x++) { dataFromFile = sr.ReadLine(); StationDescrip.Add(dataFromFile); //add each item in the file to the List<> } } } catch(Exception ex) { NotifyUser("An error occured while initializing your settings. " + ex.Message.ToString()); } //*************************WORKING WITH "CONTROL ARRAYS" AND LISTS*********************************** //Now I will make an array of the radio buttons on my form so I can assign text properties to each of them. Notice you //DO NOT use the 'new' keyword here. I want to have access to my existing controls. If I used the 'new' keyword I would //be creating new controls. I just want to reference them... access them in their current memory location. To do this, //make sure you use the 'this' keyword to tell the compiler the controls are in the currenct instance of the form class object. STEP 1: //---create a ref array to access the radio buttons on the form RadioButton[] myButtons = {this.rdoStation1, this.rdoStation2, this.rdoStation3, this.rdoStation4, this.rdoStation5, this.rdoStation6,this.rdoStation7,this.rdoStation8}; STEP 2: Cycle through the array of buttons. For each button assign the string in the List<> we created above (StationDescrip). I use a foreach loop to cycle through the array of radiobuttons and use my own counter (x) to assign values from the List<> starting with 0. int myIndex = 0; foreach (RadioButton eachButton in myButtons) { eachButton.Text = StationDescrip[myIndex]; myIndex++; } //**************************SAVE UPDATED CHANGES TO DISK************************************** private void btnUpdate_Click(object sender, EventArgs e) { //---now go the other way and assign new values to the List<> StationDescrip[0] = txt1.Text.Trim(); StationDescrip[1] = txt2.Text.Trim(); StationDescrip[2] = txt3.Text.Trim(); StationDescrip[3] = txt4.Text.Trim(); StationDescrip[4] = txt5.Text.Trim(); StationDescrip[5] = txt6.Text.Trim(); StationDescrip[6] = txt7.Text.Trim(); StationDescrip[7] = txt8.Text.Trim(); try { //---cycle through each item in the List<> StationDescrip and write to the disk file using (StreamWriter myWriter = new StreamWriter(myFileName, false)) //false means do not append data! foreach (string whichOne in StationDescrip) { myWriter.WriteLine(whichOne); } NotifyUser("Your data has been updated."); } catch (Exception ex) { NotifyUser("I could not save your changes." + ex.Message.ToString()); } } //********************Now Cycle Through The Contols and Save their Text Property values to the List<>************************** private void btnUpdate_Click(object sender, EventArgs e) { //---create a control array for the textboxes TextBox[] myTextBoxes = {this.txt1, this.txt2,this.txt3, this.txt4, this.txt5, this.txt6, this.txt7, this.txt8}; //---now go the other way and assign new values to the List<> int x = 0; foreach (TextBox eachTextBox in myTextBoxes) { StationDescrip[x] = eachTextBox.Text.Trim(); x++; } try { //---cycle through each item in the List<> StationDescrip and write to the disk file using (StreamWriter myWriter = new StreamWriter(General.myFileName, false)) //false means do not append data! foreach (string whichOne in General.StationDescrip) { myWriter.WriteLine(whichOne); } NotifyUser("Your data has been updated."); } catch (Exception ex) { NotifyUser("I could not save your changes." + ex.Message.ToString()); } }