wingZ_blade Channel
wingZ_blade Channel

Reputation: 9

C# How to send multiple values to other form?

I'm trying to send multiple values to a second form and display the text in a textbox without loosing the old values (every value in a new line), but i dont know how... Would be happy if somebody could help me :)

I searched everywhere to find a solution but couldn't find enything.

Code from data.cs
private static string Acc;
    public static string GetAcc()
    {
        return Acc;
    }
    public static void SetAcc(string value)
    {
        Acc = value;
    }

Code from Form 1
private void bunifuThinButton21_Click(object sender, EventArgs e)
    {
        Data.SetAcc(textBox1.Text);
    }

Code from Form 2
public Generated()
    {
        InitializeComponent();
        textBox1.Text = Data.GetAcc();
    }

Now if I send a second value it should be displayed in the textbox in the next line and the first value should be in the first line.

Upvotes: 0

Views: 384

Answers (1)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56717

Well, the general approach would be to use properties on the second form and set them in the calling form.

You seem to be using some kind of data holder that keeps all the data entered by the user. This is also a valid approach, but the way you implement it doesn't fit what you describe.

If you want to keep several values you either need to use several properties or (in your case) a list of entries. Maybe this could be a better approach in your case:

Code from data.cs

private static List<string> Acc = new List<string>();

public static string[] GetAccounts()
{
    return Acc.ToArray();
}

public static void AddAccount(string value)
{
    Acc.Add(value)
}

Code from Form 1

private void bunifuThinButton21_Click(object sender, EventArgs e)
{
    Data.AddAccount(textBox1.Text);
}

Code from Form 2

public Generated()
{
    InitializeComponent();
    textBox1.Text = String.Join(Environment.NewLine, Data.GetAccounts());
}

Upvotes: 2

Related Questions