Hi guys,

I Googled this till the end and can't find a solution to this. What I want to do is the following - I have Runners with attributes like their Name, ID, Position and Time. All those are added for each runner in a listbox, seperated by commas. Like such:

Joe, 0, 1, 3
Gary, 1, 3, 5
Sam, 2, 4, 4

Now, Next to the listBox are textboxes. If I click on one of these list items their Name, ID, Position and Time must show in 4 textboxes with their values respectfully.

Like such:

Name: Joe
ID: 0
Position: 1
Time: 3

I have code to show that I tried using listBox1.SelectedItems.ToString() to achieve the above mentioned but its not exactly what I want. The code:

Main Form:

namespace HW2
{
    public partial class Form1 : Form
    {
        Race myRace = new Race();
        Runner myRunner = new Runner();
        string name;

        private void button4_Click(object sender, EventArgs e)
        {
            //Adding a new runner to the race by using the AddRunnerInfo() method
            //NAME
            name = textBox4.Text;
            myRunner.Name = name;

            //ADD PARAMETERS TO THE METHOD
            myRace.AddRunnerInfo(name);

            //DISPLAY THE RUNNER'S DETAILS

            listBox1.Items.Clear();
            foreach (var runner in myRace.GetRunners())
            {
                for (int i = 0; i < 1; i++)
                {
                    listBox1.Items.Add(runner.Name + ", " + runner.RaceNumber + ", " + runner.Time + ", " + runner.Position + "\n");
                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Racenumber in textbox
            textBox3.Text = listBox1.SelectedIndex.ToString();

            //Name in textbox

            //Not working?? :(
            textBox5.Text = listBox1.SelectedItem.ToString();

            //Time in textbox
            //textBox7.Text = Convert.ToString(myRunner.mTime);

            //Position in textbox
            //textBox6.Text = Convert.ToString(myRunner.mPosition);
        }
    }
}

Class Race

namespace HW2
{
    class Race
    {
        public List<Runner> myList = new List<Runner>();

        private double mDistance;
        private string mName;
        private string mDate;
        private string mRunnerList;

        public double Distance
        {
            get { return mDistance; }
            set { mDistance = value; }
        }

        public string Name
        {
            get { return mName; }
            set { mName = value; }
        }

        public string Date
        {
            get { return mDate; }
            set { mDate = value; }
        }

        public string RunnerList
        {
            get { return mRunnerList; }
            set { mRunnerList = value; }
        }

        public void AddRunnerInfo(string name)
        {

            Runner runA = new Runner();

            runA.mName = name;

            foreach (Runner run in myList)
            {
                int number = 1;
                for (int i = 0; i < myList.Count; i++)
                {
                    runA.mRaceNumber = (number + i);
                }
            }

            myList.Add(runA);

            //TESTING THE RACENUMBER ASSIGNMENT
            //MessageBox.Show(Convert.ToString(runA.mName));
            //MessageBox.Show(Convert.ToString(runA.mRaceNumber));
        }

        public List<Runner> GetRunners()
        {
            List<Runner> returnValue = new List<Runner>();
            foreach (Runner runner in myList)
            {
                for (int i = 0; i < 1; i++)
                {
                    returnValue.Add(new Runner { Name = runner.Name, RaceNumber = runner.RaceNumber, Time = runner.Time, Position = runner.Position });
                    i++;
                }
            }
            return returnValue;
        }
    }
}

Class Runner

namespace HW2
{
    class Runner
    {
        public string mName;
        public int mPosition;
        public int mRaceNumber;
        public double mTime;

        public string Name
        {
            get { return mName; }
            set { mName = value; }
        }

        public int Position
        {
            get { return mPosition; }
            set { mPosition = value; }
        }

        public int RaceNumber
        {
            get { return mRaceNumber; }
            set { mRaceNumber = value; }
        }

        public double Time
        {
            get { return mTime; }
            set { mTime = value; }
        }

        public Runner()
        {
            mName = "No Name";
            mPosition = 0;
            mRaceNumber = 0;
            mTime = 0.0;
        }
    }
}

As I said. I really tried Googling this but cant get a solution. So any help would be much appreciated! :)

Edited 1 Year Ago by Joemeister: Corrections

Hi

A quick and dirty solution would be to simply use the String.Split method to split the string on the comma and return it to a string array. You would then assign each of the elements from the string array to each of your text boxes.

However, if you are interested in doing this in a more OOP way then try the following:

First, let's change the way that you add data to your ListBox. Basically, rather than adding strings, we will add the actual Runner object. So change your code where you loop through the GetRunners() method and add each to the ListBox to the following:

    listBox1.Items.AddRange(myRace.GetRunners().ToArray());

Now run this and have a look at the results. You will notice that it simply states HW2.Runner or something like this. This is because .NET is using the ToString method of the object to get it's name and as we haven't supplied any form of ToString method yet it is using the lower System.Object ToString method. All objects in .NET inherit from this and System.Object.ToString will simply return the full class name of the object.

So the next bit to do is to override the ToString behaviour so that you can display the object in the ListBox in the way that you had previously. In your Runner class, add the following:

public override string ToString()
{
    return string.Format("{0}, {1}, {2}, {3}", Name, RaceNumber, Time, Position);
}

Now run your program and you will see that you have the format back.

Now add the following to your SelectedIndexChanged event of the ListBox:

    MessageBox.Show(listBox1.SelectedItem.ToString());

Now run the program and select an Item. You will note that you get the same value as was selected. However, what might not be apparent immediately is that this is using the ToString() method of the selected item which is actually the Runner object. So it is the actual instance of the Runner class. So now you can do the following in your SelectedIndexChanged event:

Runner selectedRunner = (Runner)listBox1.SelectedItem;

MessageBox.Show(selectedRunner.Name);
MessageBox.Show(selectedRunner.RaceNumber.ToString());

So now you have all the data at your disposal via the selectedRunner instance and can now populate your text boxes.

HTH

Comments
Nice.
This article has been dead for over six months. Start a new discussion instead.