c#从MainForm中的Form执行一个方法

时间:2017-03-31 08:48:49

标签: c# forms winforms

嗯,我不确定标题是否真的很清楚,所以让我们用一个例子解释一下:

这是点击时打开WinForm的方法......

namespace STS
{
    public partial class MainForm : Form
    {
        /*---------- SHOW DATABASE ----------*/
        private void showDataBasesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowDataBase db = new ShowDataBase();
            db.Show();
        }
    }
}

这是WinForm退出时执行的代码。那是我希望它在MainForm中调用方法的时刻。

......这是错误......

namespace STS
{
    public partial class ShowDataBase : Form
    {
        /*----------  ----------*/
        private void ShowDataBase_FormClosed(object sender, FormClosedEventArgs e)
        {
            /*I've try this but error: An object reference is required for the non-static field, method, or property*/
            MainForm.plotMarks();

            /*And this but error: There is no argument given that corresponds to the required formal parameter*/
            MainForm mF = new MainForm();
            // With: MainForm mF = new MainForm(this); //--> No error but it execute another Form than the one I want (my splashScreen in this case)...
            mF.plotMarks()
        }
    }
}

以下是我想要的方法:

namespace STS
{
    public partial class MainForm : Form
    {
        public void plotMarks()
        {
            MessageBox.Show("Hello");
        }
    }
}

提前感谢您的帮助!

迪米特里

0 个答案:

没有答案