【问题标题】:how to return this variable from a button to be use for all the form如何从按钮返回此变量以用于所有表单
【发布时间】:2022-11-22 02:53:27
【问题描述】:

我正在尝试使用 openfileDialog 从按钮插入文件,然后返回文件名以用于以另一种形式读取文件

private void buttin1_Click(object sender, EventArgs e)
{
OpenFileDialog File = new OpenFileDialog();
var FileName = File.FileName;
return FileName; 
}

private void buttin2_Click(object sender, EventArgs e)
{
DataTable dtexcel = ReadExcel(FileName);
}



【问题讨论】:

    标签: c# excel forms variables return


    【解决方案1】:

    你不能return你的函数Button1_Click中的任何东西,最好设置一个将被你的MainWindow使用的变量

    public class MainWindow : Window
    {
        private string yourFileName{get;set;}
        //...
        private void buttin1_Click(object sender, EventArgs e)
        {
            OpenFileDialog File = new OpenFileDialog();
            this.yourFileName = File.FileName;
        }
        
        private void buttin2_Click(object sender, EventArgs e)
        {
             if(yourFileName!="" && File.Exists(yourFileName)
             {
                 DataTable dtexcel = ReadExcel(yourFileName);
             }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 1970-01-01
      • 2021-11-29
      • 2019-02-06
      • 1970-01-01
      • 1970-01-01
      • 2012-12-06
      相关资源
      最近更新 更多