//This is used to save the data records private void btnSave_Click(object sender, EventArgs e) { //Takes out only the time component of DateTime and convert to long HH:MM:SS format as required string startingTime = startTime.ToLongTimeString(); //When the Save was clicked we take down the time, as the time to enter the entry ends here string endTime = DateTime.Now.ToLongTimeString(); //Save the details from the text fields to MyObject as a record record = new MyObject(); record.MyObjectID = ctlFirstName.Text + " " + ctlMinit.Text + " " + ctlLastName.Text; record.FirstName = ctlFirstName.Text; record.LastName = ctlLastName.Text; record.Phone = ctlPhone.Text; record.Minit = ctlMinit.Text; record.Add1 = ctlAdd1.Text; record.Add2 = ctlAdd2.Text; record.City = ctlCity.Text; record.State = ctlState.Text; record.ZipCode = ctlZipCode.Text; record.Email = ctlEmail.Text; record.ProofofPurchase = checkboxProof.Checked; record.Date = ctlDate.Text; record.startTime = startingTime; record.endTime = endTime; myobjects.Add(record); //Add to ListView and make the item focused ListViewItem li = listNames.Items.Add(ctlFirstName.Text + " " + ctlMinit.Text + " " + ctlLastName.Text); li.SubItems.Add(ctlPhone.Text); //if some other item is selected, remove selection on that and select current item if (listNames.SelectedItems.Count != 0) { listNames.SelectedItems[0].Selected = false; } listNames.FocusedItem = li; li.Selected = true; //Find the item with the Text and get the index of the item var item = listNames.FindItemWithText(ctlFirstName.Text + " " + ctlMinit.Text + " " + ctlLastName.Text); int i = 0; if (item != null) { i = listNames.Items.IndexOf(item); //Make sure it is visible to the user by scrolling to it listNames.EnsureVisible(i); } //write to the data file writetotextClear(myobjects); //Clear the fields afterwards ctlFirstName.Clear(); ctlLastName.Clear(); ctlAdd1.Clear(); ctlAdd2.Clear(); ctlMinit.Clear(); ctlEmail.Clear(); ctlCity.Clear(); ctlPhone.Clear(); ctlState.Clear(); ctlZipCode.Clear(); ctlDate.ResetText(); checkboxProof.Checked = true; //acknowledge entry has been saved StatusBar.Refresh(); stripstatus.Text = "Entry Saved"; StatusBar.Refresh(); //make button false again btnSave.Enabled = false; }
public RebateForm() { //Set the state of Window to Maximized, User can Minimize this.WindowState = FormWindowState.Maximized; InitializeComponent(); //Create instance of class ListViewSorter for Column Sort lvwColumnSorter = new ListViewSorter(); //Create instance of column header ColumnHeader columnheader = new ColumnHeader(); // Size the columns with respect to data in them, Can be done in Form Load Event but refresh frequency is too much foreach (ColumnHeader ch in this.listNames.Columns) { ch.Width = -2; } //Let user move columns around this.listNames.AllowColumnReorder = true; //Use custom sorter to sort the listView this.listNames.ListViewItemSorter = lvwColumnSorter; //Creates a file, if not present. Otherwise, nothing. StreamWriter w = File.AppendText(FilePath); w.Close(); //Read all lines from File to check if any data is there in text file and store in array records. string[] records = System.IO.File.ReadAllLines(@FilePath); //Parse the lines into their individual fields. //For example, the part associated with FirstName goes to MyObject.FirstName foreach (string x in records) { string[] data = x.Split('\t'); readrecord = new MyObject(); readrecord.MyObjectID = data[0] + " " + data[2] + " " + data[1]; readrecord.FirstName = data[0]; readrecord.Minit = data[2]; readrecord.LastName = data[1]; readrecord.Add1 = data[3]; readrecord.Add2 = data[4]; readrecord.City = data[5]; readrecord.State = data[6]; readrecord.ZipCode = data[7]; readrecord.Phone = data[8]; readrecord.Email = data[9]; if (data[10] == "True") { readrecord.ProofofPurchase = true; } else { readrecord.ProofofPurchase = false; } readrecord.Date = data[11]; readrecord.startTime = data[12]; readrecord.endTime = data[13]; //Add these fields to myobjects array of MyObject myobjects.Add(readrecord); //Print the item in current listView string Phone = readrecord.Phone; string Name = readrecord.FirstName + " " + readrecord.Minit + " " + readrecord.LastName; //Adds new listview item for previous records ListViewItem item = new ListViewItem(Name); item.SubItems.Add(Phone); listNames.Items.Add(item); } }