0

私のプロジェクトには 2 つのフォームがあり、5 つのクラスが好きです。私がしたいのは、クラスのフォームで定義されているメソッドを呼び出すことです。そのため、クラスで呼び出したときに実行されます。クラスでフォームのインスタンスを作成することを考えましたが、それは「新しいフォーム」を作成するだけであり、無意味です。私の他のオプションは、フォームを静的フォームにすることですが、これを行う方法がわかりません。ご協力ありがとうございました!

編集

私のフォームで定義されているメソッドは次のとおりです。

        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);
            }
        }
    }
4

1 に答える 1

2

あなたの元のリクエストを理解していれば、非常に基本的な例です。

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();
   }
}
于 2013-04-30T13:08:42.537 に答える