private void btnsave_Click(object sender, EventArgs e) { // Save the text in txtNewUsername into the variable userId. string userId = txtNewUsername.Text; // Create a foreach statement. foreach (Control c in this.Controls) { if (c is TextBox) { // If the textbox is empty... TextBox txtBox = c as TextBox; if (txtBox.Text == string.Empty) { // If the textbox is empty, display a message prompting the userto enter a name. MessageBox.Show("You must enter a username."); return; } } } // Call the GetFileLocation() method from the File Location Manager class and save the returned information // into the variable filePath. string filePath = FileLocationManager.GetFileLocation(); // Create a try catch. try { // See if the file already exists and if it does, write the name to the file. FileStream aFile = new FileStream(filePath, FileMode.Append, FileAccess.Write); // Create a new instance of the StreamWriter class and cll it sw. Pass aFile into this class. StreamWriter sw = new StreamWriter(aFile); // Write the userId to sw. sw.WriteLine(userId); // Close sw. sw.Close(); // Close aFile. aFile.Close(); } // If there is an error, catch the error. catch (Exception ex) { Console.WriteLine(ex.StackTrace); } // Show a message box telling the user that the name has been successfully added. MessageBox.Show("User Added Succesfully."); // Reset txtNewUsername to having no characters. txtNewUsername.Text = ""; }
private double BestTime() { // Create a new variable called lowestTime and set it to 0. double lowestTime = 0; // Create an if statment. if (File.Exists(FileLocationManager.GetFileName())) { // If the file from the variable filePath exists, make a new list and call it timeArray. List <double> timeArray = new List <double>(); // Make an instance of the BinaryReader class, open the File class, go to method GetFileName() in File Location Manager, open the FileMode. using (BinaryReader b = new BinaryReader(File.Open(FileLocationManager.GetFileName(), FileMode.Open))) { // Create a new variable called pos and set it to 0; double pos = 0; // Create a new variable called length and using the instance of the BinaryReader class, open the BaseStream and get the .Length property. // Cast this so it is a double. double length = (double)b.BaseStream.Length; // While pos is less than length... while (pos < length) { // Create a new variable called v and using the instance of the BinaryReader class, open the method ReadDouble(). double v = b.ReadDouble(); // Add the time to the array. timeArray.Add(v); // pos += sizeof(double); } } // Save the minimum time in the timeArray array under the variable lowestTime. lowestTime = timeArray.Min(); } // Return the variable lowestTime. return(lowestTime); }
private void ScoreTableData(string Player, double time) { // Save the Player and time under the variable userAndTime. string userAndTime = Player + "-" + time; // Save the information from this file as the variable filePath. string filePath = FileLocationManager.GetFileTime(); // If filePath does not exist... if (!File.Exists(filePath)) { // Create a new instance of the FileStream class and call it aFile. Create a new file. FileStream aFile = new FileStream(filePath, FileMode.Create, FileAccess.Write); // Create a new instance of the StreamWriter class and call it sw. Pass aFile into this class. StreamWriter sw = new StreamWriter(aFile); // Write the userAndTime to sw. sw.WriteLine(userAndTime); // Close sw. sw.Close(); // Close aFile. aFile.Close(); } // Otherwise... else { // Create a new instance of the FileStream class and call it aFile. Add to the existing file. FileStream aFile = new FileStream(filePath, FileMode.Append, FileAccess.Write); // Create a new instance of the StreamWriter class and call it sw. Pass aFile into this class. StreamWriter sw = new StreamWriter(aFile); // Write the userAndTime to sw. sw.WriteLine(userAndTime); // Close sw. sw.Close(); // Close aFile. aFile.Close(); } }
private void ShowResults() { // Create a new List call it column and populate it with the information below. List <string> column = new List <string>() { "Name", "Time (sec)" }; // Create a new List and call it Row. List <string[]> Row = new List <string[]>(); // Create a new two-dimensional string array and call it timeArray. string[,] timeArray; // Create a new string array and call it users. string[] users = new string[0]; // If this file exists... if (File.Exists(FileLocationManager.GetFileTime())) { // Pass the information from this file into users. users = File.ReadLines(FileLocationManager.GetFileTime()).ToArray(); } // Add the information from the users array. timeArray = new string[users.Length, 2]; // Create a for loop. for (int i = 0; i < users.Length; i++) { // For every piece of information from the users array, split the name and time using a '-'. string[] userNameAndTime = users[i].Split('-'); // Assign the appropiate information to the appropiate place in the table. timeArray[i, 0] = userNameAndTime[0]; timeArray[i, 1] = userNameAndTime[1]; } // Create another for loop. for (int i = 0; i < users.Length; i++) { // Add a new row. Create a new array and display the information that was split above. Row.Add(new string[] { timeArray[i, 0], timeArray[i, 1].ToString() }); } // Create a new instance of the DataTable class and call it table. DataTable table = new DataTable(); // Create a foreach statement. foreach (string text in column) { // Add the text into the column. table.Columns.Add(text); } // Create another foreach statement. foreach (string[] cell in Row) { // Add the cell into the row. table.Rows.Add(cell); } // Show the dataScoreTable. dataScoreTable.DataSource = table.AsDataView(); dataScoreTable.Sort(dataScoreTable.Columns["Time (sec)"], ListSortDirection.Ascending); }