I am trying to the contents of my List to a listView. My list is a list of objects and is in a separate form from my ListView. Here's is a snippet of my code
<pre>public partial class Form1 : Form
{
String path = "HighScore.txt";
LeaderBoard score=new LeaderBoard();
public List<HighScore> OrdereadList = new List<HighScore>();
List<HighScore> highScores = new List<HighScore>();
public void add()
{
int count=0;
foreach (var highScore in OrdereadList)
{
count++;
Console.WriteLine("Adding stuff");
addItem(count.ToString(), highScore.Name, highScore.Level, highScore.Result, highScore.Time.ToString());
}
}
}
public void addItem(string num,string name,string level, string result,string time)
{
ListViewItem number = new ListViewItem(num);
ListViewItem.ListViewSubItem rowName = new ListViewItem.ListViewSubItem(number, name);
ListViewItem.ListViewSubItem rowLevel = new ListViewItem.ListViewSubItem(number, level);
ListViewItem.ListViewSubItem rowResult = new ListViewItem.ListViewSubItem(number, result);
ListViewItem.ListViewSubItem rowTime= new ListViewItem.ListViewSubItem(number, time);
number.SubItems.Add(rowName);
number.SubItems.Add(rowLevel);
number.SubItems.Add(rowResult);
number.SubItems.Add(rowTime);
}
And here's is my other form LeaderBoard and my attempt
public partial class LeaderBoard : Form
{
Form1 form;
public LeaderBoard(Form1 frm)
{
InitializeComponent();
form = frm;
}
public void addItem(string num, string name, string level, string result, string time)
{
ListViewItem number = new ListViewItem(num);
ListViewItem.ListViewSubItem rowName = new ListViewItem.ListViewSubItem(number, name);
ListViewItem.ListViewSubItem rowLevel = new ListViewItem.ListViewSubItem(number, level);
ListViewItem.ListViewSubItem rowResult = new ListViewItem.ListViewSubItem(number, result);
ListViewItem.ListViewSubItem rowTime = new ListViewItem.ListViewSubItem(number, time);
number.SubItems.Add(rowName);
number.SubItems.Add(rowLevel);
number.SubItems.Add(rowResult);
number.SubItems.Add(rowTime);
lstLeaderBoard.Items.Add(number);
}
private void LeaderBoard_Load(object sender, EventArgs e)
{
int count=0;
foreach(var item in form.OrdereadList)
{
count++;
addItem(count.ToString(),item.Name,item.Level,item.Result,item.Time.ToString());
}
}
}
What I have tried:
After searching online, I think I have discovered the cause of my problem during my several attempts. Any time I'm implementing my code, I create a new instance of either the LeaderBoard class or the Form1 class; one of the things I tried to fix was
lang="C#">
Form1 form;
public LeaderBoard(Form1 frm)
{
InitializeComponent();
form = frm;
}
But it didn't seem to work. I also tried placing the method that generated the items in the List for Form1 into a constructor method and then creating a new instance of form1 in my leaderBoard class, but it didn't work. I also figured since my Leader board will always be blank, I could create a new instance and add an item to it, but this didn't seem to work either.