How do i call a method from a different form?

Hi,

I have 2 forms that are closely linked with each other:

form1. a form which lists a bunch of different layers

form2. a form which describes the layer (i.e. name, colour, etc)

if i change something in form1, i want to call an update function in to update the display in form2, and vice versa.

What is the best way to do this from my knowledge, using delegate only works in one direction, correct me if i'm wrong.

My idea is to create the update functions within their respective classes and find some way to call that function from the other form. is that possible i have already set it up so that they are sharing a data structure, i just need them to access each other's functions.

thanks.



Answer this question

How do i call a method from a different form?

  • Atleta

    Mark the post as the unser of the thread.

  • Quirk

    ok, you convinced me to use delegates, but i'm kind of new to c# and i can't seem to find any examples suited for my particular situation.

    can you either post a sample code or a link for me to look at

    thanks.


  • Juergen Lorenz

  • Gentlehag

    thanks!! that's exactly what i needed... you guys are awesome.
  • Jeanet

    Indeed delegates do the best job, however there are other ways too.

    You can instantiate the second form class and call the method that way.

    Or you can create a static method which you can call directly.


  • Haiasc_123

    google for delegates, they are what you need.



  • Vlince

    //form1 code

    class form1

    {

    public delegate void MaterialDelegate(int a);

    public MaterialDelegate MaterialDel;

    public form1()

    {

    MaterialDel = new MaterialDelegate(DisplayMaterial);

    }

    public void DisplayMaterial(int materialIndex)  {...}

    public void StartForm2()

    {

    form2 f2 = new form2(this);

    f2.ShowDialog();

    }

    }

    //form2 code

    class form2

    {

    private form1 f1;

    public form2(form1 parent)

    {

    f1 = parent;

    }

    void CallParent(int index)

    {

    f1.Invoke(f1.MaterialDel, new object[] {index});

    }

    }



  • Aamir Iqbal

    ok, right now i'm trying to call a function in form2 from form1:

    LayerFrm: creates materialFrm and delegates the DisplayMaterial function to UpdateMaterialFrm

    public delegate void MaterialDelegate(int a);

    private MaterialFrm myMaterialFrm;

    myMaterialFrm = new MaterialFrm(...);

    MaterialDelegate UpdateMaterialFrm = new MaterialDelegate(myMaterialFrm.DisplayMaterial);

    UpdateMaterialFrm(sel); <<this is where i'm unsure about, how do i call the delegate >>

    MaterialFrm: has the function which is called in form1

    public void DisplayMaterial(int materialIndex) {...}

    i get the error:

    The name 'UpdateMaterialFrm' does not exist in the class or namespace 'DrawApp.LayerFrm'

    i'm really confused by this...

    thanks.


  • Jason D. Camp

    // You can instantiate the second form class and call the method that way.

    But a new instance won't point to the old form

    // Or you can create a static method which you can call directly.

    this can work if the method does not require state, or if the hwole thing can be set up so a field is the same for all instances.



  • How do i call a method from a different form?