none
c# winform - forms relationsheeps

    Question

  • I have Form1 and Form2, and from buttonClickEvent on Form1 I want to handle with DataGridView (dgv01) on Form2, then show Form2. Something like:

    Form1 - buttonClick:

    Form2.dgv01.Rows.Add(4);

    Form2.Show();

    What is the Way, pls?

    ps.

    In VBA above codes works perfectly. Why is c# so complicated?

    dimanche 15 avril 2012 07:39

Réponses

  • VBA performs in a more friendly way but not the best optimized (reference based object creation)

    In your case you have the instance of Form1 created and inside it you need to create instance of Form2.

    Double click on the button to generate the button click event handler and add the following code inside it.

    private void buttonclick(args..) { Form2 form = new Form2(); form.dgv01.Rows.Add(); // Make the dgv Modifier property as Public

    }



    Resolving n Evolving in C# (http://jeanpaulva.com)

    • Marqué comme réponse Bonaca dimanche 15 avril 2012 08:17
    dimanche 15 avril 2012 07:48

Toutes les réponses

  • VBA performs in a more friendly way but not the best optimized (reference based object creation)

    In your case you have the instance of Form1 created and inside it you need to create instance of Form2.

    Double click on the button to generate the button click event handler and add the following code inside it.

    private void buttonclick(args..) { Form2 form = new Form2(); form.dgv01.Rows.Add(); // Make the dgv Modifier property as Public

    }



    Resolving n Evolving in C# (http://jeanpaulva.com)

    • Marqué comme réponse Bonaca dimanche 15 avril 2012 08:17
    dimanche 15 avril 2012 07:48
  • Thanks Jean, especially for your note about dgv Modifier. Your code works.

    But I still wondering about c# syntax and logic. You said that optimization is the Reason. Optimize for what? If speedness is in question - I have a lot of vba projects with more then 20 forms inside, and never had problem with the speed of loading them. Or, maybe...

    Anyway, thanks once more. I will visit your blog.


    • Modifié Bonaca dimanche 15 avril 2012 08:19
    dimanche 15 avril 2012 08:17
  • Optimize for what?

    When you create a Form class, VB automatically creates a public variable with the same name as the class. Whenever you use that variable, VB checks if the variable is null and, if it is, it instantiate the class. If you use your own variable, no checks are inserted.

    lundi 16 avril 2012 11:26