I am new to C# and have started a program that I want to take in data from user, send to list box, sort list box and then save results. This happens via two text boxes:
txtUsername txtResult
below the text box is a button that is disabled unless both text fields are entered, the button (btnAdd) adds the results and name of student to two separate list boxes:
lstUsername lstResult
by disabling the button I ensure only both name and result are entered and sent to the list boxes in unison, the next bit is where it becomes tricky for me..
I want to be able to be able to sort the list boxes in ascending order to see at a glance the user with the highest result, but this wont be achieved due to the entries in each list box not being linked to one another. for example if a put a line of code to sort the high scores they then would not match the user in the list box parallel. I have only touched on arrays but understand the data between Username and result in a list box would need to be linked in some way so they are matched even after sorting.
This is the current code on the button that sends both the username and result to the list boxes:
InitializeComponent();
{
btnAdd.Enabled = !string.IsNullOrWhiteSpace(txtResult.Text);
btnAdd.Enabled = !string.IsNullOrWhiteSpace(txtUsername.Text);
}
private void btnAdd_Click(object sender, EventArgs e)
{
lstUsername.Items.Add(txtUsername.Text);
lstResult.Items.Add(txtResult.Text);
//There is also a counter that counts entries once being sent to the list
txtScoreCount.Text = lstUsername.Items.Count.ToString();
lstResult.Items.Count.ToString();
txtResult.Clear();
txtUsername.Clear();
txtUsername.Focus();
txtUsername.SelectAll();
}
If anyone can provide advice on how I am able to match the entries of two separate list boxes together I would be very grateful, I am also happy to provide more code if required. Any advice is beneficial to me. I will be able to work out saving the data and loading it back into the lists myself once I have the data in two lists bound, thank you.