C# 如何在另一个事件中使用一个事件

C# 如何在另一个事件中使用一个事件,c#,C#,我需要在另一个事件中使用一个事件。我是初学者,不知道怎么做。我想在第二个事件中使用第一个事件 //第一件事 private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Filter = "Text Document(*.txt)|*.txt|All fil

我需要在另一个事件中使用一个事件。我是初学者,不知道怎么做。我想在第二个事件中使用第一个事件

//第一件事

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "Text Document(*.txt)|*.txt|All files(*.*)|";
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
        using (StreamWriter sw = new StreamWriter(s))
        {
            sw.Write(textBox1.Text);
        }
    }

}
//第二件事

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult rezult = MessageBox.Show("Sunteti sigur ca doriti sa iesiti din program !?",
        "Aplication closing", MessageBoxButtons.YesNoCancel);
    if (rezult == DialogResult.Yes)
    {
        //I want to use first event in this if,please help me;
        e.Cancel = false;
    }
    else if (rezult == DialogResult.No)
    {
        e.Cancel = false;
    }
    else
        e.Cancel = true;
}

将该代码移动到一个单独的函数中,然后在两个事件处理程序中调用该函数。

您只需调用
this即可。SaveAstolStripMenuItem\u在
Form1\u FormClosing
方法中单击(null,null)
(因为没有实际使用参数)。但是,我强烈建议将save逻辑重构为一个单独的方法,并在适当的地方调用它,如下所示:

private void Save() 
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "Text Document(*.txt)|*.txt|All files(*.*)|";
    if (saveFileDialog1.ShowDialog() == DialogResult.OK)
    {
        using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
        using (StreamWriter sw = new StreamWriter(s))
        {
            sw.Write(textBox1.Text);
        }
    }
}

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) 
{
    this.Save();
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e) 
{
    DialogResult rezult = MessageBox.Show("Sunteti sigur ca doriti sa iesiti din program !?",
        "Aplication closing", MessageBoxButtons.YesNoCancel);
    if (rezult == DialogResult.Yes)
    {
        this.Save();
        e.Cancel = false;
    }
    else if (rezult == DialogResult.No)
    {
        e.Cancel = false;
    }
    else
        e.Cancel = true;
   }
}

对于今天,只需调用第一个函数,如下所示

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult rezult = MessageBox.Show("Sunteti sigur ca doriti sa iesiti din program !?",
        "Aplication closing", MessageBoxButtons.YesNoCancel);
    if (rezult == DialogResult.Yes)
    {
        // call other event handler as a function, for it is one
        saveAsToolStripMenuItem_Click(null, null);

        e.Cancel = false;
    }
    else if (rezult == DialogResult.No)
    {
        e.Cancel = false;
    }
    else
        e.Cancel = true;
}

明天,读一篇关于Document/View、Model/View/Controller、Model/View/ViewModel的博客/书籍/文章。

将功能提取到一个单独的函数中,并在两个事件处理程序中调用它。我解释得不是很好,我不想在两个事件中使用相同的代码,我想在关闭应用程序时保存它。但我有一个事件使用了这段代码(保存),我相信我可以在第二次事件中给他打电话。。。