Trying to access a list I've filled up in form 1 on form 2 but the list is empty?

Viewed 29

So let me start of by saying I'm extremely new to coding ( followed a course about general programming logic ) and am now messing around with c# winforms. I'm trying to create the game charades in c# winforms and have gotten pretty far already. My form 1 handles all the game setup ( number of players, naming the players, etc ... ), I have a "player" class to create objects and learn myself OOP which seems to be the general way to go with C# from what I've read. However now that I'm trying to work on form 2 it seems the list from the "player" class I've filled up in form 1 is empty when I try to access it on form 2.

Quick rundown on what I'm doing;

-Form 1 loads by default and I put in the number of players and name each player which I then add to a list through a method created in the "player" class.

-Once all players have been named form 1 closes and form 2 opens ( found a bit of code online to get this to work by making a bool switch, when it's set to true it'll execute some code in program.cs to do "Application.run(form2)" so the program can stay active when closing form 1 and showing form 2.

-On form 2 I'm trying to access the list from the "player" class which I can do without a problem by creating a new object of the "player" class in form 2 and creating a method that gets the list from that class, but when I do it's completely empty.

here's the code from form 1, form 2 and player class.

form1 :

    using System;
using System.Security.Cryptography.X509Certificates;
using System.Windows.Forms;

namespace Pictionary
{
    public partial class Form1 : Form
    {
        public bool Form2Loaded { get; set; }
        private int aantalKliks = 0;
        private int aantalSpelers = 0;

    public Spelers speler1 = new Spelers();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void pictionaryTitle_Click(object sender, EventArgs e)
    {
        
    }

    private void buttonSpelregels_Click(object sender, EventArgs e)
    {
        MessageBox.Show(
            $"1 - Kies of iedereen voor zichzelf speelt of in teams.{Environment.NewLine}2 - Geef de namen van de spelers in of een team naam per team.{Environment.NewLine}3 - Er word een speler of team gekozen om het woord uit te beelden. Druk op de knop om het woord zichtbaar/onzichtbaar te maken. Druk op de start knop om de ronde te beginnen.{Environment.NewLine}4 - De andere spelers/team hebben 60 seconden de tijd om het woord te raden. Kies de juist optie om de punten toe te voegen.{Environment.NewLine}5 - Druk op de knop stop spel om het spel te stoppen en de punten te laten zien.",
            "Spelregels");
    }

    private void buttonFreeforall_Click(object sender, EventArgs e)
    {
        flowLayoutPanel1.Show();
        buttonBack.Show();
        buttonTeams.Hide();
        buttonFreeforall.Hide();
        buttonSpelregels.Hide();
    }

    private void buttonBack_Click(object sender, EventArgs e)
    {
        flowLayoutPanel1.Hide();
        buttonBack.Hide();
        buttonTeams.Show();
        buttonFreeforall.Show();
        buttonSpelregels.Show();
        radioButton2.Checked = false;
        radioButton3.Checked = false;
        radioButton4.Checked = false;
        radioButton5.Checked = false;

    }

    private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {
        flowLayoutPanel1.Hide();
        buttonBack.Hide();
        textBox1.Show();
        buttonAddName.Show();
        aantalSpelers = 2;
    }


    private void radioButton3_CheckedChanged(object sender, EventArgs e)
    {
        flowLayoutPanel1.Hide();
        buttonBack.Hide();
        textBox1.Show();
        buttonAddName.Show();
        aantalSpelers = 3;
    }

    private void radioButton4_CheckedChanged(object sender, EventArgs e)
    {
        flowLayoutPanel1.Hide();
        buttonBack.Hide();
        textBox1.Show();
        buttonAddName.Show();
        aantalSpelers = 4;
    }

    private void radioButton5_CheckedChanged(object sender, EventArgs e)
    {
        flowLayoutPanel1.Hide();
        buttonBack.Hide();
        textBox1.Show();
        buttonAddName.Show();
        aantalSpelers = 5;
    }

    private void buttonAddName_Click(object sender, EventArgs e)
    {
        if (textBox1.Text == "" || textBox1.Text == "Speler Toegevoegd!")
        {
            MessageBox.Show("Naam mag niet blanco zijn!");
        }

        else
        {
            switch (aantalKliks)
            {
                case 0:
                    speler1.naamSpeler = textBox1.Text;
                    speler1.SpelerNaarLijst();
                    break;

                case 1:
                    speler1.naamSpeler = textBox1.Text;
                    speler1.SpelerNaarLijst();
                    break;

                case 2:
                    speler1.naamSpeler = textBox1.Text;
                    speler1.SpelerNaarLijst();
                    break;

                case 3:
                    speler1.naamSpeler = textBox1.Text;
                    speler1.SpelerNaarLijst();
                    break;

                case 4:
                    speler1.naamSpeler = textBox1.Text;
                    speler1.SpelerNaarLijst();
                    break;

            }

            textBox1.Text = "Speler Toegevoegd!";

            aantalKliks = aantalKliks + 1;
        }
        if (aantalKliks == aantalSpelers)
        {
            this.Close();
            Form2Loaded = true;

        }
    }
}

}

form 2

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Pictionary
{
public partial class Form2 : Form

#region generatedCode

{
    public Spelers speler1 = new Spelers();

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
     
    }


    private bool buttonSwitch = false;

    private void buttonToggleWord_Click(object sender, EventArgs e)
    {
        buttonSwitch = !buttonSwitch;

        if (buttonSwitch == true)
        {
            labelWoord.Text = "Woord: test";

        }

        else
        {
            labelWoord.Text = "Woord: ";
        }
    }

}
#endregion

}

player class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Pictionary
{
public class Spelers
{
    //Attributes
    public string naamSpeler;
    public List<string> spelerLijst = new List<string>();


    //constructors

    //methods
    public void SpelerNaarLijst()
    {
        spelerLijst.Add(naamSpeler);
    }``

}

}

Any help and even some explanation as to why it's not working is greatly appreciated !

0 Answers
Related