private void scoreborad_Load(object sender, EventArgs e) { try { StreamReader sr = new StreamReader(filePath); string txt = sr.ReadLine(); while (txt != null) { string[] input = txt.Split(','); Indvscore stat = new Indvscore(input[0], Convert.ToInt32(input[1])); player.Addplayer(stat); txt = sr.ReadLine(); } sr.Close(); // Refresh Data List refreshDGV(); } catch (DirectoryNotFoundException) { Directory.CreateDirectory(directory); StreamWriter sw = createFile(); sw.Close(); } catch (FileNotFoundException) { StreamWriter sw = createFile(); sw.Close(); } }
private void submitBTN_Click(object sender, EventArgs e) { Boolean nameExist = false; // Check if the user fill in the name if (nameTB.Text == "") { MessageBox.Show("Please enter your name!", "Info"); } else { // Check if the chosen name exist for (int i = 0; i < player.getSize(); i++) { if (nameTB.Text == player.getscore(i).getName()) { nameExist = true; } } if (nameExist) { MessageBox.Show("This name has been taken, please use another name!", "Info"); } else { checkSubmit = true; StreamWriter sw = createFile(); Indvscore stat = new Indvscore(nameTB.Text, Convert.ToInt32(scoreLBL.Text)); player.Addplayer(stat); // Display all the scores entered for (int i = 0; i < player.getSize(); i++) { sw.WriteLine(player.getscore(i).getName() + "," + player.getscore(i).getScore()); } sw.Close(); // Display the popup MessageBox.Show("Your score has been submitted!", "Info"); // Refresh Data List refreshDGV(); // Disable submit button submitBTN.Enabled = false; } } }
public void Addplayer(Indvscore p) { Boolean alreadyExist = false; for (int i = 0; i < scoreList.Count; i++) { if (scoreList[i].getName() == p.getName()) { scoreList[i].setScore(scoreList[i].getScore() + p.getScore()); alreadyExist = true; } } if (alreadyExist == false) { scoreList.Add(p); } }