Question
I cannot figure out how to get my update score form to grab the name from the student listbox and scores, any help would be
I cannot figure out how to get my update score form to grab the name from the student listbox and scores, any help would be appreciated. Assignment Below and code below. Can provide full project solution if needed. Removed "using" includes to save space in question.
Update students scores form needs to get the name and scores of a student based on the selected index of the main listbox and allow scores to be added, edited, deleted, or cleared.
FORM1.cs
namespace StuScores { public partial class Form1 : Form { public Form1() { InitializeComponent();
}
private List students = null;
private void populatelstbox() { lstboxStudents.Items.Clear(); foreach (Student s in students) { lstboxStudents.Items.Add(s.GetDisplayText()); } }
private void btnAdd_Click(object sender, EventArgs e) { frmAddNew addStudent = new frmAddNew(); Student student = addStudent.GetNewStudent();
if (student != null) { students.Add(student); StudentDB.SaveStudents(students); populatelstbox(); int i = students.Count - 1; lstboxStudents.SetSelected(i, true); }
}
private void btnUpdate_Click(object sender, EventArgs e) { int i = lstboxStudents.SelectedIndex; frmUpdate updateStudent = new frmUpdate(); Student student = updateStudent.StudentUpdate(); if (i != -1) { Student selectedStudent = students[i]; } }
private void btnDel_Click(object sender, EventArgs e) { int i = lstboxStudents.SelectedIndex;
if (i != -1) { Student student = students[i]; students.Remove(student); StudentDB.SaveStudents(students); populatelstbox(); txtScoreTotal.Text = String.Empty; txtScoreCount.Text = String.Empty; txtAverage.Text = String.Empty; }
}
private void btnExit_Click(object sender, EventArgs e) { Application.Exit(); } private void Form1_Load(object sender, EventArgs e) { students = StudentDB.GetStudents(); populatelstbox(); lstboxStudents.SelectedIndexChanged += new EventHandler(lstboxStudents_SelectedIndexChanged);
}
private void DoScores() { int i = lstboxStudents.SelectedIndex;
if (i != -1) { Student selectedStudent = students[i]; if (selectedStudent.ScoreList.Count() == 0) { txtScoreTotal.Text = "No Scores"; txtScoreCount.Text = "No Scores"; txtAverage.Text = "No Scores"; } else { int total = 0; foreach (int score in selectedStudent.ScoreList) { total += score; } int count = selectedStudent.ScoreList.Count(); decimal average = Convert.ToDecimal(total) / Convert.ToDecimal(count);
txtScoreTotal.Text = Convert.ToString(total); txtScoreCount.Text = Convert.ToString(count); txtAverage.Text = average.ToString("#.###");
} }
}
private void lstboxStudents_SelectedIndexChanged(object sender, System.EventArgs e)
{ //MessageBox.Show("changed"); DoScores();
}
} }
StudentDB.cs
namespace StuScores { public static class StudentDB { private const string dir = @"../../../../StudentScores.txt";
public static void SaveStudents(List students) { StreamWriter textOut = new StreamWriter( new FileStream(dir, FileMode.Create, FileAccess.Write));
foreach (Student student in students) { string scoresList = "";
for (int i = 0; i
scoresList += (value + ","); }
textOut.Write(student.Name + "|"); textOut.WriteLine(scoresList.TrimEnd(',')); } textOut.Close(); }
public static List GetStudents() { StreamReader textIn = new StreamReader( new FileStream(dir, FileMode.OpenOrCreate, FileAccess.Read));
List students = new List();
while (textIn.Peek() != -1) { string row = textIn.ReadLine(); string[] columns = row.Split('|'); Student student = new Student();
List scoreList = new List(); if (columns[1] != "") { int[] scoreArray = columns[1].Split(',').Select(n => Convert.ToInt32(n)).ToArray();
scoreList = scoreArray.ToList(); student.ScoreList = scoreList;
} else { student.ScoreList = new List(); }
student.Name = columns[0]; students.Add(student);
}
textIn.Close();
return students; }
} }
Student.cs
namespace StuScores { public class Student { private string name; private List scoreList;
public Student() { }
public Student(string name, List scoreList) { this.Name = name; this.ScoreList = scoreList; }
public string Name { get { return name; } set { name = value; } }
public List ScoreList { get { return scoreList; } set { scoreList = value; } }
public string GetDisplayText() { string total = ""; foreach (int score in scoreList) { //MessageBox.Show(score.ToString());
total += "|" + score; } return name + total; } } }
frmUpdate.cs
namespace StuScores { public partial class frmUpdate : Form { public frmUpdate() { InitializeComponent(); } private Student student = null; public Student StudentUpdate() { this.ShowDialog(); return student; }
private void frmUpdate_Load(object sender, EventArgs e) {
}
private void frmUpdate_Activated(object sender, EventArgs e) {
}
} }
For this project, you'll develop an application that let the user maintain a list of student scores. For this project, though, you'll use a text file to store the student data. The design of the Student Scores form Student Scores - x Students: Add New... Joel Muroch1977183 Doug Lowe99193197 Anne Boehm 100 100 100 Update... Delete Score total: Score count: Average: 251 3 83 Exit Starting Screen Add New Student Name: Score: Add Score Scores: Clear Scores OK Cancel Add New Student Screen Update Student Scores Name: Scores: Ist Student Scores Add Update Remove Clear Scores OK Cancel Update Student Scores Add Score Score: Add Cancel Add Score Screen (Get here from Add Score Button on Add New Student or add scores for the update scores screen). Specifications When this application starts, it should read student data from a text file named StudentScores.txt you will find this on blackboard When students are added, updated, or deleted, this application should write the student data to the Student Scores.txt file, overwriting any existing data. This application should use a class named Student DB to read data from and write data to the Student Scores.txt fileStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started