C#委托,保存文件

委托小例子:

        //定义委托

        private delegate void ChangeImage(string fileName);

        //定义委托要绑定的方法

        private void ChangePicBox(string fileName)
        {
            if (fileName != string.Empty)
                this.pictureBox1.Image = Image.FromFile(fileName);
            else
                this.pictureBox1.Image = null;
        }

        //声明委托的变量

        private ChangeImage changePic;

        //实例化委托的变量,将委托绑定要执行的方法

        changePic = new ChangeImage(ChangePicBox);

        //调用invoke方法,执行声明的委托变量类型

       this.pictureBox1.Invoke(changePic, fileNamePicture[0]);



        //保存文件的例子代码

        try
        {
             SaveFileDialog savefile = new SaveFileDialog();
             savefile.Filter = "Excel 文档(*.xlsx)|*.xlsx";
             savefile.RestoreDirectory = true;
             savefile.OverwritePrompt = true;
             if (savefile.ShowDialog() == DialogResult.OK)
             {
                if (myExcel.SaveAs(savefile.FileName))
                {
                    MessageBox.Show("保存成功", "提示");
                    myExcel.Close(false);
                 }
              }
          }
          catch (Exception er)
          {
              MessageBox.Show(er.Message);
          }
          finally
          {
          }


你可能感兴趣的:(C#,delegate,委托)