Flame
Flame

Reputation: 35

Passing data from multiple userControls to a form

I need to make a scoreboard with names.

On the first form you add players VIA a user control.

Button1_click adds a UserControl1 into a panel. UserControl1 simply has a textbox, label and a button, when text has been inserted and the button (inside UserControl1) is pressed, text is transferred into the label and the button and textbox are disposed.

Now, when the user is finished adding enough players, he presses the second button, which would open the second form, with one panel.

Now this panel needs a second userControl, in that there is simply a label. (this has to be a userControl since later on i'll add score) So now, the program must read, all the names that the user had added in Form1 add them into the userControl2 label and add that userControl into Form2's panel

I hope this made sense.

This is what i have done so far:

Form1 code

public List<PlayerAdder> PlayerList { get; set; } = new List<PlayerAdder>();

    private void B_AddPlayer_Click(object sender, EventArgs e)
    {
        count++;
        PlayerAdder PA = new PlayerAdder();
        PlayerList.Add(PA);           
        for (int J = 0; J < count; J++)
        {
            GBX_Title.Text = "Mängijate Arv: " + count;
            flowLayoutPanel1.Controls.Add(PA);
        }      
    }

As you can see I have made a list, into which it should store the names.

I also have a CodeFile which contains:

 public class MyData
{
    public List<Players> Players { get; set; }
}
public class Players
{
    public string PlayerName { get; set; }
}

the first UserControl which should send the name to the list :

    public string PlayerName
    {
        get { return NameInput.Text; }
        set { NameInput.Text = value; }
    }

Form2 would have this code :

 private void PlayerNames() 
    {
        for (int i = 0; i < gameInfo.Players.Count; i++)
        {
            ScoreboardMenuControl SMC = new ScoreboardMenuControl();
            flowlayoutpanel.Controls.Add(SMC);
        }
    }

and the last ScoreboardMenuControl I haven't figured anything out yet.. This is a lot to digest but hopefully you understand. Here's a a visual on what i want to do

From1 -> UserControl1 -> Data

Form1 -> Form2 -> Panel -> UserControl2 -> Data

Upvotes: 0

Views: 508

Answers (1)

wnvko
wnvko

Reputation: 2120

So you should pass your Data from Form1.UserControl1 to Form2.UserControl2. I would suggest you do this via Form2 constructor like this:

public Form2(MyData data)
{
    this.InitializeComponent();
    this.userControl2.Data = data;
}

Then in Form1 you can do something like this:

var form2 = new Form2(this.userControl11.Data);

However, this will tight couple Form2 to Form1. To avoid this you can pass the data through interfaces like this. First, create IMyData interface like this:

public interface IMyData
{
    List<Player> Players { get; set; }
}

Then your MyData class should implement this interface. Then in Form2 constructor wait for the IMyData interface instead of concrete implementation like this:

public Form2(IMyData data)
{
    this.InitializeComponent();
    this.userControl2.Data = data;
}

This will help you also later when you test your project to mock MyData.

Upvotes: 1

Related Questions