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! :)