listbox selection update textbox

Need to update 3 text boxes with decimals, after selecting an item in a Listbox.

link to files: https://www.dropbox.com/s/xj2efe5sxsolswk/midterm.zip

Format in listbox: "Name |XX| |XX| |XX|" e.g. "Matt |100| |90| |80|"

How do I recall the 3 values associated with a selected index from the listbox, to calculate 3 values and update 3 text boxes, associated with that listbox selection?

I have created 3 lists to attempt to store what I need. I am saving the score inputs to scoreList2, but not sure how to link them when associating it with the ListBox.

    public static List<string> scoreList = new List<string>();
    public static List<decimal> scoreList2 = new List<decimal>();
    public static List<object> scoreList3 = new List<object>();

Code:

    private void Form1_Load(object sender, EventArgs e)
    {
        (all hard coded cuz I'm a noob)
        //txtScoreTotal.Text = tempNum1.ToString();
        //txtScoreTotal.text = 
        //txtScoreCount.Text = tempNum2.ToString();
        //txtAverage.Text = tempNum3.ToString();

        lbStudents.Items.Add(tempInfo1 + " " + tempNum1 + " " + tempNum2 + " " + tempNum3);

    }

    private void lbStudents_SelectedIndexChanged(object sender, EventArgs e)
    {
        /*
        txtScoreTotal.Text = tempNum1.ToString();
        //txtScoreTotal.Text = selected listbox scoretotal
        txtScoreCount.Text = tempNum2.ToString();
        //txtScoreTotal.Text = selected listbox scorecount
        txtAverage.Text = tempNum3.ToString();
        //txtSCoreTotal.Text = selected listbox average
         */

        txtScoreTotal.Text = lbStudents.SelectedItem.ToString();

    }

Answers


You can use String.Split method and LINQ like this:

private void lbStudents_SelectedIndexChanged(object sender, EventArgs e)
{
   if(lbStudents.SelectedItem != null)
   {
       decimal result;
       var numbers = lbStudents.SelectedItem.ToString()
       .Split(new [] { '|' }, StringSplitOptions.RemoveEmptyEntries)
       .Where(x => decimal.TryParse(x, out result))
       .ToList();

       txtBox1.Text = numbers[0];
       txtBox2.Text = numbers[1];
       txtBox3.Text = numbers[2];
   }

}

Need Your Help

Play two different sound files from two different buttons?

java android file audio button

I tried searching for this in many places, but could not find an answer. I am making an app with a set of image buttons that have the alphabet on them. I want to be able to click the letter A, and ...

Partial mocking of class with Moq

c# .net mocking moq

I want to mock only the GetValue method of the following class, using Moq: