0

Possible Duplicate:
Passing Data Between Forms

Im new to Visual Studio 2012 & programming in c#, so excuse me if my question is trivial.

I have form1 that is getting quite full so I have add a second form (form2) I have a click button event that will display it, but I cannot transfer info between the forms. In the file Form1.cs it does not seem to know about Form2 & vice versa.

   private void button4_Click(object sender, EventArgs e)
   {
       OneDimLife.Form2 Form = new OneDimLife.Form2();
       Form.Show();
       //int i, j, N, M;
       //N = panel1.Width; M = panel1.Height;

   }

I probably want panel1 on form1 to be copied to panel1 on form2 (maybe even tripled in size).

Community
  • 1
  • 1

2 Answers2

3

Don't do that. Have a common instance of a class that holds the data. Show the bits of it you want on each form. The route you are going down always ends up in a confusing mess for all but the most trivial of implementations.

Better still of course would be an interface, but that's a bit further into your future.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
1

Here is the code on your first form (SimpleForm):

        Form2 frm = new Form2();
        frm.SourceForm = this;
        frm.ShowDialog();

And the code on the second one (Form2):

public partial class Form2 : Form
{
    public SimpleForm SourceForm { get; set; }

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        //i'am changing Title property on first form...
        SourceForm.Text = "Changed title on SourceForm";
    }
}
Erre Efe
  • 15,387
  • 10
  • 45
  • 77
Gregor Primar
  • 6,759
  • 2
  • 33
  • 46
  • Mark down was a bit harsh, as it does answer the question. Got to say though for all you learners. Don't do this, at home, school or work, you will get hurt. Common data class or an interface. THis sort of thing is bad enough with straight classes, with forms it's a disaster of biblibical proportions. – Tony Hopkinson Sep 03 '12 at 22:02
  • Randolf: the 14122 reference is actually on the page that Andre suggested. Indeed I have worked through these examples & now can confidently pass data from form to form using 3 differents methods. Gregor: I could not make head nor tail of your answer; I'm an amateur who needs lots of explaination! Tony: Your comments made me laugh out loud. Could you give me a reference to interfaces ? or might that lead to a disaster of biblical proportions. All: My question is answered; bless you all DeeFlatCoda :+) – DeeFlatCoda Sep 03 '12 at 23:47
  • Tony could you explain why do you think this practise is so bad? I must say I use this concept more than 10 years inside small and medium projects and I had never experienced any problems. This is as simplest solution to the problem as it gets. I prefer not to kill a small bird with a cannon. – Gregor Primar Sep 04 '12 at 05:10
  • @Gregor leaving aside it's a violation of just about every OO principle. For instnce pass a and b to form 2, they impact c and d on which gets passed back to form 1, which impact a and b. It's a very fragile mechanism, one or two apparently unrealted changes to the form can leave you in a right mess, and one you might not discover for a while. – Tony Hopkinson Sep 04 '12 at 17:05
  • @DeeFlatCoda It's in the books, but takes a bit to get your head round. If you used a common class to hold the data, both your forms would have to know about it. If you wanted to implement that class differently, now you have three places to change. If however you use an interface (think of it as a contract), then you can implement it with many classes and your forms won't care which one because they only know the interface. – Tony Hopkinson Sep 04 '12 at 17:10