avatarbobo
avatarbobo

Reputation: 269

How to call a method that is on a Form from a class?

I have 2 forms on my project and like 5 classes. What I want to do is to call a method that is defined on a Form in a class, so it runs when I call it on my class. I thought about creating an instance of my Form in my class, but that would only create a "new Form" and it would be pointless. My other option is to make my form a static form, but I don't know how to do this. Thank you for your help!

EDIT

Here's the method that is defined on my Form:

        public void fillMatrix(char[,] currentMatrix, int rows, int columns)
    {
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                string route = GameInstance.vexedGame.imgToShow(currentMatrix[i, j]);
                DataGridVexed[j, i].Value = Bitmap.FromFile(route);
            }
        }
    }

Upvotes: 0

Views: 148

Answers (1)

Mesh
Mesh

Reputation: 6442

Very basic example, if I've understood your original request:

public class MyForm : Form
{
    public A a = null;
    public MyForm ()
    {
        A = new A(this); // pass an instance of the MyForm to the class
    }

    public void WowMethod(){
       ... something amazing ...
    }
}

public class A
{
   public MyForm associatedForm = null;

   public A( MyForm f ){
      associatedForm = f;
   }

   public void CallWowMethod()
   {
      associatedForm.WowMethod();
   }
}

Upvotes: 2

Related Questions