0

I want to transfer data from Form2 into Form1's listBox1. Form2 includes several textboxes,

I have tried to face this problem by placing all of this code into Form1 but I cannot get the values needed from Form2.

Can anybody help, I understand I'm not very clear but I can answer any questions about the code.

private void button3_Click(object sender, EventArgs e)
{
    List<Form1Other> FileList = new List<Form1Other>();
    Form1Other[] f1Other = new Form1Other[10];
    Form1 testForm1 = new Form1();

    string TName = textBox1.Text;
    string TDesc = textBox2.Text;
    decimal TPrior = numericUpDown1.Value;
    string TDate = dateTimePicker1.Value.ToShortDateString();
    string TCompl = "UNFINISHED";

    FileList.Add(new Form1Other(TName, TDesc, TPrior, TDate, TCompl));

    testForm1.listBox1.Items.Add(FileList[0].tName);
    testForm1.listBox1.Items.Add(FileList[0].tDesc);
    testForm1.listBox1.Items.Add(FileList[0].tPrior);
    testForm1.listBox1.Items.Add(FileList[0].tDate);
    testForm1.listBox1.Items.Add(FileList[0].tCompl);
    System.Diagnostics.Debug.WriteLine(FileList[0].tDesc);
Alex
  • 13,024
  • 33
  • 62
bhryne
  • 9
  • 1
  • You want data from Form2 in Form1? Are they different classes? If so, can you set a public property in form2 then get it in form1? – Jonathan Kittell Apr 26 '15 at 00:30
  • Where in your code example is `Form2`? What is `Form1Other`? What good does it do to retrieve values from a brand-new instance of `Form1Other`? I.e. how did _its_ values get initialized? If the code you posted is already in `Form1`, why are you creating a new instance of `Form1`? Why are you retrieving values from the controls in the current form (which you say is `Form1`), rather than from an instance of `Form2`? You are right...your question is far from clear. You will need to improve it **a lot** to get a good answer. – Peter Duniho Apr 26 '15 at 01:15
  • **Note:** that duplicate question was asked/answered a long time ago and since then, many other similar questions have been asked and answered. But that one is pretty good as far as it goes; I've just added a new answer to elaborate on a comment someone else made on the accepted answer, to show an alternative strategy. In your scenario, either of those answers should apply. Note that both assume that at some point, one form object has a reference to the other; you didn't post enough code here for anyone to suggest how to do that in your case, but that shouldn't be hard for you to figure out – Peter Duniho Apr 26 '15 at 01:44

1 Answers1

-1

You most likely need some sort of messaging/event mechanism so you can communicate across different views/forms.

You either need to implement your own using a Queue and Publish/Subscribe. System.Messaging

Or use one that is already implemented.

A few to note that I use are:

Michal Ciechan
  • 13,492
  • 11
  • 76
  • 118