Click here to Skip to main content
15,881,863 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
C#
<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);

            //score.lstLeaderBoard.Items.Add(number);

        }

And here's is my other form LeaderBoard and my attempt
C#
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.
Posted
Updated 11-Feb-22 18:50pm

1 solution

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

In your case, it's probably the first one you need to read most.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900